我可以根据Chart.js中的给定阈值设置不同的填充颜色吗?

时间:2014-12-12 08:34:25

标签: javascript chart.js

Chart.js(Chart.js)中是否可以为高于或低于给定阈值的值设置不同的填充颜色?

我在Chart.js中找不到任何选项,有没有人之前做过这样的事情?我更喜欢在Highcharts上使用Chart.js。

像这样:Highcharts example

HTML

<script src="http://code.highcharts.com/highcharts.js"></script>

<div id="container" style="height: 400px"></div>

JS

$(function () {
    $('#container').highcharts({
        chart: {
            type: 'area'
        },
        xAxis: {
            categories: ['Jan', 'Feb', 'Mar', 'Apr', 'May', 'Jun']
        },
        yAxis: {
            title: {
                text: '$'
            }
        },
        title: {
            text: 'Win/Loss'
        },
        series: [{
            name: 'Avg. ($)',
            data: [546, 342, -234, -540, 103, -345],
            threshold: -1,
            negativeFillColor: 'rgba(255,0,0,0.5)',
            fillColor: 'rgba(0,204,0,0.5)',
            lineColor: 'rgba(0,0,0,1)',
            marker: {
                fillColor: 'rgba(255,255,255,1)',
                lineColor: 'rgba(0,0,0,1)',
                lineWidth: 1
            }
        },{
            name: 'User ($)',
            data: [-640, -665, -700, -640, -630, -623],
            fillOpacity: 0,
            lineColor: 'rgba(20, 94, 204, 1)',
            marker: {
                fillColor: 'rgba(255,255,255,1)',
                lineColor: 'rgba(20, 94, 204, 1)',
                lineWidth: 1
            }
        }]
    });
});

1 个答案:

答案 0 :(得分:0)

我使用gradientStroke命令做了类似的事情,然后将其应用于backgroundColor:

var ctx = document.getElementById("YearsExperience").getContext("2d");
var gradientStroke = ctx.createLinearGradient(0, 1200, 0, 0);
gradientStroke.addColorStop(0, "#80b6f4");
gradientStroke.addColorStop(0.2, "#94d973");
gradientStroke.addColorStop(0.4, "#80b6f4");
gradientStroke.addColorStop(1, "#94d973");
var YearsExperience = new Chart(ctx, {
    type: 'bar',
    data: { labels: ArrLabel[idx], datasets: [{ label: 'Years of experience', data: ArrData[idx], backgroundColor: gradientStroke }] },
    options: { scales: { xAxes: [{gridLines: {display:false}}], yAxes: [{ ticks: { beginAtZero:true } }] }, events: ['none'],animation: { easing: "easeInOutBack" } }
    });

可以在这里看到输出示例:http://neophytte.mine.nu/portfolio/skills.html