chartist icon indicating copy to clipboard operation
chartist copied to clipboard

Create bands on line charts

Open Theadd opened this issue 9 years ago • 17 comments

Is it possible to limit the area drawn by a serie to the value of another serie as in grafana or dygraphs?

Theadd avatar May 19 '15 14:05 Theadd

That's not possible as of yet. Let's track this as an enhancement.

gionkunz avatar May 23 '15 21:05 gionkunz

I would also be interested by that. I'm trying to implement it, but I don't well see how area is drawn using SVG... (and I think it's not that easy :) )

maftieu avatar Jun 03 '15 09:06 maftieu

We had a similar discussion already here #283 and I've produced an example with masking http://jsbin.com/yahuzun/4/edit?css,js,output which was only to test the mechanism.

After closer consideration I believe that the easiest way to enable users to build band area charts would be to add an option to Chartist that allows you to associate two series together to form a band.

I have something like this in mind:

var chart = new Chartist.Line('.ct-chart', {
  labels: ['1', '2', '3', '4', '5', '6', '7', '8', '9', '10', '11', '12'],
  series: [
    {
      name: 'max',
      data: [12, 9, 7, 8, 5, 4, 6, 2, 3, 3, 4, 6]
    },
    {
      name: 'min',
      data: [4,  5, 3, 7, 3, 5, 5, 3, 4, 4, 5, 5]
    }
  ]
}, {
  bands: [
    {lower: 'min', upper: 'max'}
  ]
});

This would still draw the regular series min and max (if not prevented with showLine and showPoint set to false) but would also create a band on the line chart canvas that is constructed from the series referenced by name in the bands objects. Its also possible to add options into the bands objects and of course create multiple bands.

Thoughts?

Btw. I renamed the issue to something more generic.

gionkunz avatar Jun 06 '15 17:06 gionkunz

Since you're taking a closer look at it, let me show you a really nice implementation of this feature (scroll down).

I know that chartist is not intended to provide that kind of time-serie data on the x-axis but I don't really want it

Also, most common usage of this feature would be something like this, which I think you already have it in mind since you've said that it'd be possible to create multiple bands.

bands: [
  {lower: 'mean', upper: 'max'},
  {lower: 'min', upper: 'mean'}
]

Theadd avatar Jun 07 '15 18:06 Theadd

I thought that it could be to use the areaBase option for an area to define the lower bound of the area.

new Chartist.Line('.ct-chart', {
  labels: [1, 2, 3, 4, 5, 6, 7, 8],
  series: [
    {
      name: 'mini',
      data: [0, 0, 0, 0, 0, 1, 3, 4, 5, 6]
    },
    {
      name: 'maxi',
      data: [5, 2, 3, 4, 6, 8.5, 11, 13, 15, 16]
    }
  ]
}, {
  low: -2,
  // showLine: false,
  showPoint: false,
  series: {
    'maxi': {
      showArea: true,
      areaBase: 'mini'
    },
  }
});

This way, it's easy to keep Chartist draw lines or points for each serie.

maftieu avatar Jun 08 '15 12:06 maftieu

@maftieu that's also an interesting approach! But then it will be hard to attach band options like custom class name etc. I also thought of something like this:

var chart = new Chartist.Line('.ct-chart', {
  labels: ['1', '2', '3', '4', '5', '6', '7', '8', '9', '10', '11', '12'],
  series: [
    {name: 'max', data: [12, 9, 7, 8, 5, 4, 6, 2, 3, 3, 4, 6]},
    {name: 'avg', data: [6, 7, 5, 7.5, 4, 3, 5.5, 2, 3, 3, 4, 6]},
    {name: 'min',  data: [4,  5, 3, 7, 3, 5, 5, 3, 4, 4, 5, 5]}
  ]
}, {
  plugins: [Chartist.plugins.ctBands({
      bands: [
        {lower: 'avg', upper: 'max', className: 'avg-to-max'},
        {lower: 'min', upper: 'avg', className: 'avg-to-min'}
      ],
      removeSeries: ['avg']
  })]
})

Where removeSeries would remove those series (including points, lines and regular areas) after the bands were created. I want to make sure someone can still show some lines and / or points if desired but can also remove series that were only used to draw the bands.

I think this functionality fits nicely into a plugin. What do you think? It's just a matter of including an other script and referencing the plugin on the chart.

gionkunz avatar Jun 10 '15 07:06 gionkunz

It certainly looks promising! I still wonder if there's a possibility this way to make a chart like this: http://jsbin.com/suquco/1/edit?html,js,console,output without too much hassle.

Martinomagnifico avatar Jun 10 '15 08:06 Martinomagnifico

@Martinomagnifico for this use-case we can easily make the areaBase configurable per series with the series config object and then just have regular step line charts with two different bases (one at 0 and the other at 100). I can add this change for the series based areaBase value into 0.9.0.

gionkunz avatar Jun 10 '15 08:06 gionkunz

I think this functionality fits nicely into a plugin.

Yeah! It'd be even better!

Theadd avatar Jun 10 '15 08:06 Theadd

Wow, that would be very cool!!!

Martinomagnifico avatar Jun 10 '15 11:06 Martinomagnifico

What is the progress on this?

mitar avatar Dec 11 '15 20:12 mitar

Hi Gion,

I'm still struggling with the masking plugin. Would it be an option to use (svg) clipPath instead of mask? And how would that be defined? Imagine a funnel-shape, starting at 10 at left and diverging to the right, (to 30 and 250 for example) consisting of multiple lines (either stacked or not). Would the clip path then be a clip path of the whole group where the highest and lowest points of it are the highest and lowest points of all lines?

The masking thing had funny side effects in combination with transparency, so I hope using clip-path does allow that.

Martinomagnifico avatar Jan 12 '16 16:01 Martinomagnifico

Bountysource

mitar avatar May 04 '16 18:05 mitar

Any progress on this?

mitar avatar Aug 01 '16 08:08 mitar

Hi, any news on this?

joserobleda avatar Jul 06 '17 11:07 joserobleda

We have recently updated the stack and backbone of Chartist, and we're ready again to work on enhancements and plugins. Just wanted to know if this is still desired.

gionkunz avatar Aug 09 '22 09:08 gionkunz

Yes, to me that would be awesome.

mitar avatar Aug 09 '22 16:08 mitar