I suggest to implement Charts Field module as replacement of current Charts Blocks module, and try to describe why:

At now Charts have Charts Blocks module, that implement charts blocks with custom data, entered manually. But very often custom charts needed for users not as block in site theme, but in separate entities (nodes, taxonomy terms, etc).

Here is manual how to implement chart as field https://www.drupal.org/docs/8/modules/charts/charts-howtos-0/how-to-add-... but, as I think, this is too hard and expensive way! We need additional Block Field module, that link to Block instance entity, that store chart data, instead of store chart data directly in this field.

Also using Block entity as storage for chart data is not good, because in Drupal 8 blocks are only instances of the block plugin entity, so it must store only display settings of real entity in current block place of Blocks layout. But we are using it for store real chart data, defined by user - I think that this is wrong!

Charts Blocks module implements only one real Charts block (charts_block) block, that store noting, and all user data store in instances of those block. Defined in module blocks are useful only when they provide dynamic info, generated via PHP, like Recent comments, Who's new, etc. But Charts block is only placeholder, that do nothing.

So better is to implement Block plugin entity for store user defined charts data, that will be available in Block types of Custom block library (/admin/structure/block/block-content/types).

But if we implement custom block type, we must add field to it, that will store chart data, instead of altering Block plugin entity properties. So, we must implement chart field type for this.

As result, right way is implement separate field type Chart field, that will store custom chart data, entered by user. In field data we can store series and data, in field display settings - chart library, type and display settings.

After implementing this, we can attach charts not only to blocks, but to any other Drupal entities without limits and separate modules like Block Field.

What do you think about this idea and implementation?

Comments

Murz created an issue. See original summary.

murz’s picture

Title: Implement Charts Field module intead of Block instance » Implement Charts Field module as replacement of Charts Blocks module
murz’s picture

And, I think, not all users of Charts module need to use Charts into Views, most of sites need charts for visualize only custom data, entered manually by users.

So, if we implement Charts Field, maybe also will be good to move Charts Views plugin from core Charts module to separate Charts Views module, for make core module easier?

andileco’s picture

Title: Implement Charts Field module as replacement of Charts Blocks module » Implement a chart-generating field

Thanks @Murz for the time you have put into thinking about the charts module! I have renamed this issue because I do not think that the block implementation should be removed. There are three (at least) use cases that the charts module should cater to:

1) Views or API implementation for plotting data across internal entities or external datasources. For example, I have a site with numerous dashboards that uses Charts.

2) Field implementation, for when a chart would be closely associated with an entity type, such as a node, across a number of those entity types. For example, if one created a content type with a chart field, then one would expect that all of the nodes of that content type might have a chart that goes with them.

3) Block implementation, where a user wants a one-off chart that can be placed on any page, but doesn't necessarily have the data structure in the site to support a view as a block. This would also be useful for someone using the layout builder and settings tray.

So, we should make this issue about adding a field that creates a chart. Please note that the latest -dev has a sub-module called "charts_fields" and already contains one field to assist with scatter plots.

murz’s picture

Great, that charts_fields already in development! I will try to use and extend it.

About "Charts Blocks" - now it implements Block instance entity type, but accept user defined data for chart (together with display settings) - I think that this is wrong.

Block instance is only view of some real data, so if we use "Charts Blocks" for render some views data - it's good, but for import chart data manually - wrong :)

So current Charts Blocks for represent view results is good, but now Charts Blocks already supports Views data as source? Seems not, only manual input.

And for manual chart data input - right way is create custom block with "charts_fields" field, containing needed data, and creating instance (one or several) of this block via "Charts Blocks" module, instead of current way via storing chart data in block instance together with display settings.

As result, current way via implementing chart field is right, and in future - will be good to integrate Charts Blocks with it too, replacing current "not-right" way of storing data.

murz’s picture

I install "charts_fields" module from git, but can't find the Chart field type, when creating new field in entity (eg node). In "charts_fields" module code I see only Views plugin for field and other views binding, but no real Drupal field binding. Does it not implemented now, or I see wrong?

murz’s picture

Title: Implement a chart-generating field » Implement a chart field with custom chart data storage
nikathone’s picture

Implementing the charts field plugin in charts core would allow might help with the fix of #3060732: Move Charts Blocks to Charts Core by allowing user to add this new field type to a block type/bundle and use the new field widget to create charts blocks. I will check the current work in the chart fields and see if I can move this work forward.

nikathone’s picture

Assigned: Unassigned » nikathone

I have been looking around to check which field storage we can use as an example and I came across the metatag contrib module and layout builder module in core. These two modules store complex data and I think we can use the same approach to come up with the chart field storage. So I think our next step would be:

  1. Update the issue to reflect what our next move
  2. Create the chart field type
  3. Create the chart simple field widget which behave like the current chart block form
  4. Create the chart field formatter

I think after this we can have follow up issues which can include:

  • Create a "fancy" chart field widget
  • Look into deprecating chart block and implement an upgrade path

This weekend I will start to work on creating the field type and the simple widget and hopefully have something for people to review by monday.

murz’s picture

@nikathone, did you have any progress with implementing chart field? I can test it in my developing site.

andileco’s picture

Version: 8.x-3.x-dev » 8.x-4.x-dev
nikathone’s picture

First pass on the implementation of the field type and field widget. Will be uploading today a following patch with the formatter and hopefully with with a configured block bundle which have the chart field to replace the current block config chart.

nikathone’s picture

Status: Active » Needs review
StatusFileSize
new21.04 KB

Here is another patch which contains the field type, widget and formatter. Next would be to:

  1. create the block chart bundle and then add the chart field to it
  2. create an upgrade path between the current block chart configuration to this one
  3. clean up the block charts code
  4. create a follow up issue to for data entry improvement
andileco’s picture

The patch applies and works for me. My priorities for the next steps would be:

4, 3, 1, 2. However, I think those can be moved into other issues. There are a few code nitpicks that I might want to make, so leaving as "Needs Review" for now, but I doubt any substantial changes are needed.

Thanks for your hard work on this, @nikathone!

andileco’s picture

StatusFileSize
new4.38 KB

Added a new patch with a few small fixes, mostly getting the attributes right in the template_preprocess_charts_item hook. I did see this (following) on line 33 of ChartConfigItemDefaultFormatter.php:

$elements[$delta] = $this->viewElement($item);

that PhpStorm claimed was unhandled.

@nikathone, how does this work for you?

  • andileco committed 12ec4eb on 8.x-4.x authored by nikathone
    Issue #3032305 by nikathone, andileco: Implement a chart field with...
andileco’s picture

Status: Needs review » Fixed

Created https://www.drupal.org/project/charts/issues/3104993 and am making this issue fixed.

Status: Fixed » Closed (fixed)

Automatically closed - issue fixed for 2 weeks with no activity.