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

javascript - Chart.js - Where to put global chart configuration? - Stack Overflow

programmeradmin0浏览0评论

I am trying to set up a Chart.js line graph with the following code. Where in this do I place the Chart.defaults.global = {} or Chart.defaults.global.responsive = true; code?

Chart.js docs can be found here: /

<!-- Chart.js -->
<script src=".js/0.2.0/Chart.min.js"></script>
<script>
    var ctx = document.getElementById("myChart").getContext("2d");
    var data = {
       labels: ["Week 1", "Week 2", "Week 3", "Week 4", "Week 5", "Week 6", "Week 7"],
       datasets: [
           {
               label: "My Second dataset",
               fillColor: "rgba(151,187,205,0.2)",
               strokeColor: "rgba(151,187,205,1)",
               pointColor: "rgba(151,187,205,1)",
               pointStrokeColor: "#fff",
               pointHighlightFill: "#fff",
               pointHighlightStroke: "rgba(151,187,205,1)",
               data: [86, 74, 68, 49, 42]
           }
       ]
   };

   var options = {
       ...
   };

   var myLineChart = new Chart(ctx).Line(data, options);
</script>

I am trying to set up a Chart.js line graph with the following code. Where in this do I place the Chart.defaults.global = {} or Chart.defaults.global.responsive = true; code?

Chart.js docs can be found here: http://www.chartjs.org/docs/

<!-- Chart.js -->
<script src="http://cdnjs.cloudflare.com/ajax/libs/Chart.js/0.2.0/Chart.min.js"></script>
<script>
    var ctx = document.getElementById("myChart").getContext("2d");
    var data = {
       labels: ["Week 1", "Week 2", "Week 3", "Week 4", "Week 5", "Week 6", "Week 7"],
       datasets: [
           {
               label: "My Second dataset",
               fillColor: "rgba(151,187,205,0.2)",
               strokeColor: "rgba(151,187,205,1)",
               pointColor: "rgba(151,187,205,1)",
               pointStrokeColor: "#fff",
               pointHighlightFill: "#fff",
               pointHighlightStroke: "rgba(151,187,205,1)",
               data: [86, 74, 68, 49, 42]
           }
       ]
   };

   var options = {
       ...
   };

   var myLineChart = new Chart(ctx).Line(data, options);
</script>
Share Improve this question edited Jan 8, 2016 at 12:28 bardzusny 3,8287 gold badges31 silver badges30 bronze badges asked Sep 8, 2014 at 12:40 user2608266user2608266 1411 gold badge1 silver badge7 bronze badges
Add a comment  | 

3 Answers 3

Reset to default 9

There are two ways to accomplish what you're looking for. This way

var myLineChart = new Chart(ctx).Line(data, options);
var options = Chart.defaults.global = {
    responsive: true,
    maintainAspectRatio: true  
};

or, this way

var myLineChart = new Chart(ctx).Line(data, {
    responsive: true,
    maintainAspectRatio: true
});

However, to make it responsive you'll have to add some extra CSS

canvas{
    width: 100% !important;
    height: auto !important;
}

so there's no need for inline CSS

Working demo

In accordance with the comment of @JustinXL below and the latest version of Chart.js there's no need for extra CSS, so check out the updated version.

Updated demo

You could set the options of your chart, but you can also set the global (as you asked) to avoid setting this specific property for each chart you want to instantiate.

You just have to set

Chart.defaults.global.elements.responsive = true;

before instantiating the chart, in your case, before

var myLineChart = new Chart(ctx).Line(data, options);

(P.S.: Chartjs V2)

In Chartjs v4.4 (I don't recall the exact version where it changed), global defaults are now under a different namespace. For example, Chart.defaults.elements.line.borderWidth = 3; (note the lack of global in the namespace.)

Another Implementation Method

There are myriad ways to implement global defaults, but one way that has worked well for me is to put them in a separate chart defaults file. I typically call mine chartDefaults.js. For example,

import Chart from "chart.js/auto"

// bar
Chart.defaults.elements.bar.borderWidth = 3;
Chart.defaults.elements.bar.borderRadius = 3;

// line
Chart.defaults.elements.line.borderWidth = 3;
Chart.defaults.elements.line.content = 'normal';

// point
Chart.defaults.elements.point.pointStyle = 'circle';
Chart.defaults.elements.point.radius = 0;
Chart.defaults.elements.point.hoverRadius = 0;

Then in each chart you instantiate, you import the defaults file (the exact implementation depends on the project structure):

import '../assets/chartDefaults'

You can still override these defaults for an individual Chart object if you need to.

发布评论

评论列表(0)

  1. 暂无评论