Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Can't change color line with chart.js

Tags:

ajax

chart.js

I use chart.js to make a line chart but I can't change the color line of my chart, I saw it grey.

My code is...

    $(document).ready(function(){
$.ajax({
url: "chart/maderas_Chart.php",
method: "GET",
success: function(data) {
  console.log(data);
  var hora = [];
  var valor = [];

  for(var i in data) {
    hora.push("Hora " + data[i].hora_nueva);
    valor.push(data[i].valor);
  }

  var chartdata = {
    labels: hora,
    datasets : [
      {
        label: 'Dique Las Maderas',
        fill: false,
        boderColor: "#bae755",
        borderDash: [5, 5],
        strokeColor: "#e755ba",
        pointColor: "#55bae7",
        pointStrokeColor: "#55bae7",
        pointHighlightFill: "#55bae7",
        pointHighlightStroke:"#55bae7",
        data: valor
      }
    ]
  };


  var ctx = document.getElementById("maderas_Chart");
  var LineGraph = new Chart(ctx, {
    type: 'line',
    data: chartdata,
    options: {
            responsive: true,
            title:{
                display:true,
                text:'Dique Las Maderas'
            },
            tooltips: {
                mode: 'index',
                intersect: false
            },
            hover: {
                mode: 'nearest',
                intersect: true
            },
            scales: {
                xAxes: [{
                    display: true,
                    scaleLabel: {
                        display: true,
                        labelString: 'Hora'
                    }
                }],
                yAxes: [{
                    display: true,
                    scaleLabel: {
                        display: true,
                        labelString: 'Valor'
                    }
                }]
            }
        }
  });
},
error: function(data) {
  console.log(data);
}
});
});

When I first confgure the chart I can change the colors but when I add more code I can't anymore, I delete all code and still can't change the line color. I'm using chart.js version 2.7, and jquery.

Thanks in advance...

like image 495
CVB Avatar asked Sep 19 '17 13:09

CVB


People also ask

What is CTX in chart JS?

js convention is to call it ctx . An object literal containing the data and the configuration options that Chart. js will use to build your chart. The required properties are type and data . In our example type is 'line' because we want a line chart.

How do you make a line graph in ChartJS?

When we're creating a chart using the Chart. js framework, we're going to need a canvas element. The Chart JS library relies on canvas elements. So create a canvas element in the HTML section, give it an ID of line-chart , and then close off that canvas element.


1 Answers

This is because, you are using deprecated properties (used in ChartJS 1.x) for setting colors. Use the following properties instead (which is applicable for ChartJS 2.7) :

datasets: [{
   label: 'Dique Las Maderas',
   fill: false,
   borderColor: "#bae755",
   borderDash: [5, 5],
   backgroundColor: "#e755ba",
   pointBackgroundColor: "#55bae7",
   pointBorderColor: "#55bae7",
   pointHoverBackgroundColor: "#55bae7",
   pointHoverBorderColor: "#55bae7",
   data: valor
}]
like image 74
ɢʀᴜɴᴛ Avatar answered Sep 20 '22 16:09

ɢʀᴜɴᴛ