在Chart.js中设置图表标题,x轴名称和y轴?

时间:2015-01-12 21:05:40

标签: charts title chart.js

Chart.js(documentation)是否有数据集选项来设置图表的名称(标题)(例如我的城市中的温度),x轴的名称(例如天)和y轴的名称(例如温度) )。或者我应该用css解决这个问题?

var lineChartData = {
    labels : ["January","February","March","April","May","June","July"],
    datasets : [
        {
            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 : [data]
        }
    ]

}

真的感谢您的帮助。

6 个答案:

答案 0 :(得分:120)

在Chart.js版本2.0中,可以为轴设置标签:

options = {
  scales: {
    yAxes: [{
      scaleLabel: {
        display: true,
        labelString: 'probability'
      }
    }]
  }     
}

有关详细信息,请参阅Labelling documentation

答案 1 :(得分:3)

如果您已经为轴设置了标签,例如@andyhasit和@Marcus提到的方式,并且想在以后更改它,则可以尝试以下操作:

chart.options.scales.yAxes[ 0 ].scaleLabel.labelString = "New Label";

完整配置供参考:

var chartConfig = {
    type: 'line',
    data: {
      datasets: [ {
        label: 'DefaultLabel',
        backgroundColor: '#ff0000',
        borderColor: '#ff0000',
        fill: false,
        data: [],
      } ]
    },
    options: {
      responsive: true,
      scales: {
        xAxes: [ {
          type: 'time',
          display: true,
          scaleLabel: {
            display: true,
            labelString: 'Date'
          },
          ticks: {
            major: {
              fontStyle: 'bold',
              fontColor: '#FF0000'
            }
          }
        } ],
        yAxes: [ {
          display: true,
          scaleLabel: {
            display: true,
            labelString: 'value'
          }
        } ]
      }
    }
  };

答案 2 :(得分:3)

对于 Chart.js 版本 3

options = {
  scales: {
    y: [{
      title: {
        display: true,
        text: 'Your Title'
      }
    }]
  }     
}

答案 3 :(得分:1)

只需使用:

<script>
  var ctx = document.getElementById("myChart").getContext('2d');
  var myChart = new Chart(ctx, {
    type: 'bar',
    data: {
      labels: ["1","2","3","4","5","6","7","8","9","10","11",],
      datasets: [{
        label: 'YOUR LABEL',
        backgroundColor: [
          "#566573",
          "#99a3a4",
          "#dc7633",
          "#f5b041",
          "#f7dc6f",
          "#82e0aa",
          "#73c6b6",
          "#5dade2",
          "#a569bd",
          "#ec7063",
          "#a5754a"
        ],
        data: [12, 19, 3, 17, 28, 24, 7, 2,4,14,6],            
      },]
    },
    //HERE COMES THE AXIS Y LABEL
    options : {
      scales: {
        yAxes: [{
          scaleLabel: {
            display: true,
            labelString: 'probability'
          }
        }]
      }
    }
  });
</script>

答案 4 :(得分:1)

对于 2021 年的读者

版本

from random import random
def random_steps(n):
    right = 0
    left = 0
    for i in range(n):
        r = random()
        print(r)
        if r > 0.5:
            right += 1
        else:
            left += 1
    print("L = {}".format(right-left))

真实世界工作示例:

"chart.js": "^3.3.2",

答案 5 :(得分:0)

          <Scatter
            data={data}
            // style={{ width: "50%", height: "50%" }}
            options={{
              scales: {
                yAxes: [
                  {
                    scaleLabel: {
                      display: true,
                      labelString: "Probability",
                    },
                  },
                ],
                xAxes: [
                  {
                    scaleLabel: {
                      display: true,
                      labelString: "Hours",
                    },
                  },
                ],
              },
            }}
          />