javascript - 使用 Chartist.js 如何更改圆环图笔划的颜色?

标签 javascript jquery charts donut-chart chartist.js

您好,我正在尝试使用 Chartist.js 创建以下圆环图:

GOAL CHART

这是图表当前的样子:

Chartist.js Donut Chart

我正在尝试找到在哪里或如何更改此图表的颜色以匹配第一个圆环图。红色和粉红色似乎是默认设置。我一直无法找到有关如何实现此目标的任何文档。我还想自定义笔划的大小和图表本身的大小。非常感谢任何帮助!

当前代码:

// ** 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>

最佳答案

所以我想通了...

我不得不进入 css 并覆盖默认值。我必须确保在 Chartist 的 cdn 之后加载了 css 文件。然后设置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;
}

然后我必须将 donutWidth 键添加到图表对象以设置笔划宽度:

var chart = new Chartist.Pie('.ct-chart', {
  series: [7, 3],
  labels: [1, 2]
}, {
  donut: true,
  donutWidth: 42,
  showLabel: false
});

关于javascript - 使用 Chartist.js 如何更改圆环图笔划的颜色?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35402143/

相关文章:

javascript - 转到 Typescript Source Definition 而不是 Visual Studio Code 中的 Compiled Definition

javascript - 宽度为 100% 的 CSS 元素落在视口(viewport)之外

javascript - 带日期的 Highcharts 气泡图?

javascript - 如何在jtree搜索过滤器jquery中播种结果多词组合匹配(jtree中的多词和条件)

javascript - Html5节点图?

java - Vaadin 提供实时图表吗?

javascript - XPath 表达式在 Chrome 中返回 'illegal string specified' 而不是 FF

javascript - css 动画移动页面 - 使用 css 修复当前元素上的页面滚动位置?

javascript - 如何使用 jQuery 基于 localStorage 预选择选择框和复选框

javascript - jQuery 在过滤器上展开可折叠 ListView