最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - Default visual style in Chart.js bar chart - Stack Overflow

programmeradmin0浏览0评论

The doc gives a little example about bar chart:

var data = {
    labels : ["January","February","March","April","May","June","July"],
    datasets : [
        {
            fillColor : "rgba(220,220,220,0.5)",
            strokeColor : "rgba(220,220,220,1)",
            data : [65,59,90,81,56,55,40]
        },
        {
            fillColor : "rgba(151,187,205,0.5)",
            strokeColor : "rgba(151,187,205,1)",
            data : [28,48,40,19,96,27,100]
        }
    ]
}

and then to create the chart using new Chart(ctx).Bar(data);

Is it possible to define the view separately from the data i.e. define fillColor and strokeColor somewhere else leaving only data in datasets objects? Assume I want only one set in datasets.

I want to generate the data (labels and values) in back-end, and leave the view details to the front-end.

The official options don't expect this.


Doc:

The doc gives a little example about bar chart:

var data = {
    labels : ["January","February","March","April","May","June","July"],
    datasets : [
        {
            fillColor : "rgba(220,220,220,0.5)",
            strokeColor : "rgba(220,220,220,1)",
            data : [65,59,90,81,56,55,40]
        },
        {
            fillColor : "rgba(151,187,205,0.5)",
            strokeColor : "rgba(151,187,205,1)",
            data : [28,48,40,19,96,27,100]
        }
    ]
}

and then to create the chart using new Chart(ctx).Bar(data);

Is it possible to define the view separately from the data i.e. define fillColor and strokeColor somewhere else leaving only data in datasets objects? Assume I want only one set in datasets.

I want to generate the data (labels and values) in back-end, and leave the view details to the front-end.

The official options don't expect this.


Doc: http://www.chartjs/docs/#barChart

Share Improve this question asked Jun 17, 2013 at 19:15 Binary BobBinary Bob 3373 silver badges10 bronze badges
Add a ment  | 

2 Answers 2

Reset to default 5

You are correct in that chartjs does not support those properties as global options.

Fortunately, a quick fix would require you to only change source lines including...

data.datasets[i].fillColor

...to, for instance:

config.fillColor || data.datasets[i].fillColor

...resulting in something similar to:

ctx.fillStyle = config.fillColor || data.datasets[i].fillColor;

The same applies to strokeColor.

Then you can either add a fillColor property to the global config or include it in datasets.

You can pute options based on ChartJS' options on the server as well, if needbe and pass that as a parameter to

new Chart(ctx).Bar(data,options);

like

new Chart(ctx).Bar(data,{fillColor : "rgba(151,187,205,0.5)", strokeColor :"rgba(151,187,205,1)",});

because ChartJS does a merge of options in

function mergeChartConfig(defaults,userDefined){
    var returnObj = {};
    for (var attrname in defaults) { returnObj[attrname] = defaults[attrname]; }
    for (var attrname in userDefined) { returnObj[attrname] = userDefined[attrname]; }
    return returnObj;
}

You can edit that to give you the effect you want! https://github./nnnick/Chart.js/blob/master/Chart.js

发布评论

评论列表(0)

  1. 暂无评论