图表js相同标签,多数据

时间:2016-11-18 10:08:43

标签: chart.js

嘿,伙计们感谢阅读和帮助...

我有图表栏使用chart.js,我在js小提琴上发现了类似的东西 这是链接http://jsfiddle.net/uh9vw0ao/

var chartData = {
    labels: ["January", "February", "March", "April", "May", "June"],
    datasets: [
        {
            fillColor: "#79D1CF",
            strokeColor: "#79D1CF",
            data: [60, 80, 81, 56, 55, 40]
        }
    ]
};

var ctx = document.getElementById("myChart1").getContext("2d");
var myLine = new Chart(ctx).Line(chartData, {
    showTooltips: false,
    onAnimationComplete: function () {

        var ctx = this.chart.ctx;
        ctx.font = this.scale.font;
        ctx.fillStyle = this.scale.textColor
        ctx.textAlign = "center";
        ctx.textBaseline = "bottom";

        this.datasets.forEach(function (dataset) {
            dataset.points.forEach(function (points) {
                ctx.fillText(points.value, points.x, points.y - 10);
            });
        })
    }
});

var ctx = document.getElementById("myChart2").getContext("2d");
var myBar = new Chart(ctx).Bar(chartData, {
    showTooltips: false,
    onAnimationComplete: function () {

        var ctx = this.chart.ctx;
        ctx.font = this.scale.font;
        ctx.fillStyle = this.scale.textColor
        ctx.textAlign = "center";
        ctx.textBaseline = "bottom";

        this.datasets.forEach(function (dataset) {
            dataset.bars.forEach(function (bar) {
                ctx.fillText(bar.value, bar.x, bar.y - 5);
            });
        })
    }
});

问题是我如何在现有数据旁边增加价值,例如:

example please click..

非常感谢!

1 个答案:

答案 0 :(得分:0)

一种非常快速和肮脏的方法是使用一个配对数组,该数组具有您想要在同一索引点处为每个数据点显示的值

即。 var supportingData = [12, 14, 15, 16, 17, 26];

然后在onAnimation中完成使用当前数据集的索引同时从这个数组中抽出数据

dataset.bars.forEach(function(bar, index) {
        //keep a refence of the fillstyle to change back to later
        var ctxColour = ctx.fillStyle;
        ctx.fillText(bar.value, bar.x, bar.y - 5);
        ctx.fillStyle = "#FF0000";

        ctx.fillText("$" + supportingData[index], bar.x + (ctx.measureText(bar.value).width)+10, bar.y - 5);
        //reset the fill style
        ctx.fillStyle = ctxColour;
});

因为这是chartjs 1.x我不认为你给图表的额外数据会传递到实际的图表,所以如果你想要更好的东西,你需要扩展自己的图表并允许它采取额外的支持数据,但显示方法相同



var chartData = {
  labels: ["January", "February", "March", "April", "May", "June"],
  datasets: [{
    fillColor: "#79D1CF",
    strokeColor: "#79D1CF",
    data: [60, 80, 81, 56, 55, 40]
  }]
};
//add an array to have your paird data in
var supportingData = [12, 14, 15, 16, 17, 26];

var ctx = document.getElementById("myChart2").getContext("2d");
var myBar = new Chart(ctx).Bar(chartData, {
  showTooltips: false,
  onAnimationComplete: function() {

    var ctx = this.chart.ctx;
    ctx.font = this.scale.font;
    ctx.fillStyle = this.scale.textColor
    ctx.textAlign = "center";
    ctx.textBaseline = "bottom";

    this.datasets.forEach(function(dataset) {
      dataset.bars.forEach(function(bar, index) {
        //keep a refence of the fillstyle to change back to later
        var ctxColour = ctx.fillStyle;
        ctx.fillText(bar.value, bar.x, bar.y - 5);
        ctx.fillStyle = "#FF0000";
        
        ctx.fillText("$" + supportingData[index], bar.x + (ctx.measureText(bar.value).width)+10, bar.y - 5);
        //reset the fill sty;e
        ctx.fillStyle = ctxColour;
      });
    })
  }
});

<script src="https://rawgit.com/nnnick/Chart.js/v1.0.2/Chart.min.js"></script>
<canvas id="myChart2" height="300" width="500"></canvas>
&#13;
&#13;
&#13;