Mark Boulder
Mark Boulder

Reputation: 14307

Why are the default Chart.js legend boxes transparent rectangles?

Why are the default Chart.js legend boxes transparent rectangles like these:

enter image description here

How do I make them solid squares like these instead? I've looked through http://www.chartjs.org/docs/latest/configuration/legend.html but can't find anything relevant.

enter image description here

https://jsfiddle.net/askhflajsf/7yped1d5/ (uses the latest master branch build)

var barChartData = {
  labels: ["2013-03-09", "2013-03-16", "2013-03-23", "2013-03-30", "2013-04-06"],
  datasets: [{
    borderColor: "#3e95cd",
    data: [10943, 29649, 6444, 2330, 36694],
    fill: false,
    borderWidth: 2
  },
  {
    borderColor: "#ff3300",
    data: [9283, 1251, 6416, 2374, 9182],
    fill: false,
    borderWidth: 2
  }]
};

Chart.defaults.global.defaultFontFamily = "'Comic Sans MS'";
// Disable pointers
Chart.defaults.global.elements.point.radius = 0;
Chart.defaults.global.elements.point.hoverRadius = 0;

var ctx = document.getElementById("bar-chart").getContext("2d");
new Chart(ctx, {
  type: 'line',
  data: barChartData,
  options: {
    responsive: true,
    legend: {
      display: true,
      position: "right"
    },
    title: {
      display: false
    },
    scales: {
      xAxes: [{
        type: "time",
        ticks: {
          minRotation: 90
        }
      }]
    }
  }
});
<script src="http://www.chartjs.org/dist/master/Chart.bundle.min.js"></script>
<canvas id="bar-chart"></canvas>

Upvotes: 1

Views: 2735

Answers (1)

ɢʀᴜɴᴛ
ɢʀᴜɴᴛ

Reputation: 32879

This is because you haven't set the backgroundColor property for your datasets (which is responsible for the legend­'s fill color).

datasets: [{
   backgroundColor: "#3e95cd",
   borderColor: "#3e95cd",
   data: [10943, 29649, 6444, 2330, 36694],
   fill: false,
   borderWidth: 2
}, {
   backgroundColor: "#ff3300",
   borderColor: "#ff3300",
   data: [9283, 1251, 6416, 2374, 9182],
   fill: false,
   borderWidth: 2
}]

ᴡᴏʀᴋɪɴɢ ᴇxᴀᴍᴘʟᴇ ⧩

var barChartData = {
   labels: ["2013-03-09", "2013-03-16", "2013-03-23", "2013-03-30", "2013-04-06"],
   datasets: [{
      backgroundColor: "#3e95cd",
      borderColor: "#3e95cd",
      data: [10943, 29649, 6444, 2330, 36694],
      fill: false,
      borderWidth: 2
   }, {
      backgroundColor: "#ff3300",
      borderColor: "#ff3300",
      data: [9283, 1251, 6416, 2374, 9182],
      fill: false,
      borderWidth: 2
   }]
};

Chart.defaults.global.defaultFontFamily = "'Comic Sans MS'";
// Disable pointers
Chart.defaults.global.elements.point.radius = 0;
Chart.defaults.global.elements.point.hoverRadius = 0;

var ctx = document.getElementById("bar-chart").getContext("2d");
new Chart(ctx, {
   type: 'line',
   data: barChartData,
   options: {
      responsive: true,
      legend: {
         display: true,
         position: "right"
      },
      title: {
         display: false
      },
      scales: {
         xAxes: [{
            type: "time",
            ticks: {
               minRotation: 90
            }
         }]
      }
   }
});
<script src="http://www.chartjs.org/dist/master/Chart.bundle.min.js"></script>
<canvas id="bar-chart"></canvas>

Upvotes: 6

Related Questions