Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Using Chartist.js how do you change the color of the stroke for a donut chart?

Hello I am trying to create the following donut chart using Chartist.js:

GOAL CHART

This is what the chart looks like currently:

Chartist.js Donut Chart

I am trying to find where or how I can change the colors of this chart to match the 1st donut chart. The red and pink seem to be the defaults. I haven't been able to find any documentation of how to accomplish this goal. I would also like to customize the size of the stroke and the size of the chart itself. Any help is greatly appreciated!

Current code:

// ** START CHARTIST DONUT CHART ** //
var chart = new Chartist.Pie('.ct-chart', {
  series: [70, 30],
  labels: [1, 2]
}, {
  donut: true,
  showLabel: false
});
chart.on('draw', function(data) {
  if(data.type === 'slice') {
    // Get the total path length in order to use for dash array animation
    var pathLength = data.element._node.getTotalLength();

    // Set a dasharray that matches the path length as prerequisite to animate dashoffset
    data.element.attr({
      'stroke-dasharray': pathLength + 'px ' + pathLength + 'px'
    });
    // Create animation definition while also assigning an ID to the animation for later sync usage
    var animationDefinition = {
      'stroke-dashoffset': {
        id: 'anim' + data.index,
        dur: 1000,
        from: -pathLength + 'px',
        to:  '0px',
        easing: Chartist.Svg.Easing.easeOutQuint,
        // We need to use `fill: 'freeze'` otherwise our animation will fall back to initial (not visible)
        fill: 'freeze'
      }
    };
    // If this was not the first slice, we need to time the animation so that it uses the end sync event of the previous animation
    if(data.index !== 0) {
      animationDefinition['stroke-dashoffset'].begin = 'anim' + (data.index - 1) + '.end';
    }
    // We need to set an initial value before the animation starts as we are not in guided mode which would do that for us
    data.element.attr({
      'stroke-dashoffset': -pathLength + 'px'
    });
    // We can't use guided mode as the animations need to rely on setting begin manually
    // See http://gionkunz.github.io/chartist-js/api-documentation.html#chartistsvg-function-animate
    data.element.animate(animationDefinition, false);
  }
});
// ** END CHARTIST DONUT CHART ** //

HTML:

<div class="ct-chart ct-perfect-fourth"></div>
like image 482
Crystal O'Mara Avatar asked Feb 15 '16 05:02

Crystal O'Mara


People also ask

How to draw a donut using CSS?

The donut is drawn using arc strokes for maximum freedom in styling */ .ct-series-a .ct-slice-donut { /* give the donut slice a custom colour */ stroke: blue; /* customize stroke width of the donut slices in CSS. Note that this property is already set in JavaScript and label positioning also relies on this.

Is it possible to expand Chartist's color palette to ~8 colors?

@gionkunz is there any way we can expand Chartist's color palette to ~8 colors? 4 is fine for line charts, but not for bar charts and pie charts where there are very frequently more than 4 pieces of data. Sorry, something went wrong. Just add more colors in your Sass settings file.

What is the default color of a chart stroke?

The red and pink seem to be the defaults. I haven't been able to find any documentation of how to accomplish this goal. I would also like to customize the size of the stroke and the size of the chart itself. Any help is greatly appreciated!

How to get started with Chartist?

The easiest way to get started with Chartist.js is by using bower: The bower package contains the JavaScript library, CSS as well as the Sass (SCSS) files. You can then integrate the desired dependencies in your project and start using them immediately. One, two, three, CSS!


3 Answers

So I figured it out...

I had to go into css and override the defaults. I had to make sure that the css file was loaded after the cdn for Chartist. Then just set width and height of ct-chart.

.ct-series-a .ct-bar, .ct-series-a .ct-line, .ct-series-a .ct-point, .ct-series-a .ct-slice-donut {
    stroke: #0CC162;
}
.ct-series-b .ct-bar, .ct-series-b .ct-line, .ct-series-b .ct-point, .ct-series-b .ct-slice-donut {
    stroke: #BBBBBB;
}
.ct-chart {
    margin: auto;
    width: 300px;
    height: 300px;
}

Then I had to add donutWidth key to the chart object to set the stroke width:

var chart = new Chartist.Pie('.ct-chart', {
  series: [7, 3],
  labels: [1, 2]
}, {
  donut: true,
  donutWidth: 42,
  showLabel: false
});
like image 54
Crystal O'Mara Avatar answered Oct 22 '22 13:10

Crystal O'Mara


A little later here, but you can provide class names to the data series to allow you to change the colors on each graph independently:

From the docs:

The series property can also be an array of value objects that contain a value property and a className property to override the CSS class name for the series group.

Instead of:

series: [70, 30]

Do this:

series: [{value: 70, className: 'foo'}, {value: 30, className: 'bar'}]

and then you can style however you'd like with the stroke css property

like image 14
Jeremy Thomas Avatar answered Oct 22 '22 15:10

Jeremy Thomas


Chartist relies on modifying CSS to control the colors, sizes, etc. of the charts. I'd suggest having a look at the documentation here to learn lots of cool tips and tricks: https://gionkunz.github.io/chartist-js/getting-started.html

But to your specific question, here's an except from the above link that tells you how to control the donut chart:

/* Donut charts get built from Pie charts but with a fundamentally difference in the drawing approach. The donut is drawn using arc strokes for maximum freedom in styling */
.ct-series-a .ct-slice-donut {
  /* give the donut slice a custom colour */
  stroke: blue;
  /* customize stroke width of the donut slices in CSS. Note that this property is already set in JavaScript and label positioning also relies on this. In the right situation though it can be very useful to style this property. You need to use !important to override the style attribute */
  stroke-width: 5px !important;
  /* create modern looking rounded donut charts */
  stroke-linecap: round;
}
like image 9
Ageonix Avatar answered Oct 22 '22 15:10

Ageonix