Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Chartjs v2 xAxes label overlap with scaleLabel

The chart js v2 is overlapping with is there a way to move the labelString of scaleLabel further down so that it does not overlap.Please view the screen shot marked in yellow and red part.

Part of the code is as following

                    scales: {
                            xAxes: [{
                                display: true,
                                ticks: {
                                    autoSkip: false,
                                    autoSkipPadding: 20
                                },
                                scaleLabel: {
                                    display: true,
                                    labelString: "ProductName(ProductName)"
                                }
                            }],

enter image description here

like image 996
Thunder Avatar asked Jan 21 '16 09:01

Thunder


1 Answers

There are two possible solutions to your problem:

1: This is for a line chart, but can easily tailored to a bar chart.

The legend is part of the default options of the ChartJs library. So you do not need to explicitly add it as an option.

The library generates the HTML. It is merely a matter of adding that to the your page. For example, add it to the innerHTML of a given DIV. (Edit the default options if you are editing the colors, etc)

<div>
    <canvas id="chartDiv" height="400" width="600"></canvas>
    <div id="legendDiv"></div>
</div>

<script>
   var data = {
        labels: ["January", "February", "March", "April", "May", "June", "July"],
        datasets: [
            {
                label: "The Flash's Speed",
                fillColor: "rgba(220,220,220,0.2)",
                strokeColor: "rgba(220,220,220,1)",
                pointColor: "rgba(220,220,220,1)",
                pointStrokeColor: "#fff",
                pointHighlightFill: "#fff",
                pointHighlightStroke: "rgba(220,220,220,1)",
                data: [65, 59, 80, 81, 56, 55, 40]
            },
            {
                label: "Superman's Speed",
                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: [28, 48, 40, 19, 86, 27, 90]
            }
        ]
    };

    var myLineChart = new Chart(document.getElementById("chartDiv").getContext("2d")).Line(data);
    document.getElementById("legendDiv").innerHTML = myLineChart.generateLegend();
</script>

2: Adding a legend template in chart options

You'll also need to add some basic css to get it looking ok.

//legendTemplate takes a template as a string, you can populate the template with values from your dataset 
var options = {
  legendTemplate : '<ul>'
                  +'<% for (var i=0; i<datasets.length; i++) { %>'
                    +'<li>'
                    +'<span style=\"background-color:<%=datasets[i].lineColor%>\"></span>'
                    +'<% if (datasets[i].label) { %><%= datasets[i].label %><% } %>'
                  +'</li>'
                +'<% } %>'
              +'</ul>'
  }

  //don't forget to pass options in when creating new Chart
  var lineChart = new Chart(element).Line(data, options);

  //then you just need to generate the legend
  var legend = lineChart.generateLegend();

  //and append it to your page somewhere
  $('#chart').append(legend);

Either of these two options will work.

In your case the legend code will look something like this

(Assuming you already have a BarChart Generated)

<div id="legendDiv"></div>
document.getElementById("legendDiv").innerHTML = BarChart.generateLegend();

Then use css to format the legend however you would prefer (including adding spacing between the legend in the chart)

like image 69
user2263572 Avatar answered Oct 13 '22 14:10

user2263572