Can't change color line with chart.js
Asked Answered
N

2

20

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...

Novelette answered 19/9, 2017 at 13:14 Comment(0)
C
36

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
}]
Camiecamila answered 19/9, 2017 at 13:18 Comment(0)
O
0

For chart.js v4 you can set borderColor property on your datasets in RGB format.

datasets: [{
        label: 'Dique Las Maderas';
        data: valor;
        borderColor: 'rgb(186, 231, 85)'; // this will change the line colour.
    }]
Olmstead answered 4/3 at 19:13 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.