javascript - 在 chart.js 中的图例上显示点样式

标签 javascript charts chart.js

https://jsfiddle.net/43Tesseracts/qmhhc089/

对于此图表的第一个数据集 XPData,如何设置图例样式以使用点而不是线?

我希望我需要做的就是根据 Scatter Line Chart docs 添加:showLines: false , 但我不知道该把这个设置放在哪里。

这是数据集(见 fiddle ):

var XPData = {
  label: 'XP Earned',
  //fill: false,
  //backgroundColor: "rgba(0,0,0,0)",
  //borderColor: "rgba(0,0,0,0)",
  pointBorderColor: "#444",
  pointBackgroundColor: "#444",
  data: [],
  showLines: false,
};

/*jshint esversion: 6 */

var ctx = document.getElementById("myChart");

class DataPoint {
  constructor(x, y) {
    this.x = x;
    this.y = y;
  }
}

var days = 85;
var chillax = 72.5;

// XP DATA SET 
var XPData = {
  label: 'XP Earned',
  //fill: false,
  //backgroundColor: "rgba(0,0,0,0)",
  //borderColor: "rgba(0,0,0,0)",
  pointBorderColor: "#444",
  pointBackgroundColor: "#444",
  data: [],
  showLines: false,

  options: {
    legend: {
      labels: {
        usePointStyle: true
      }
    }
  },
};

// XP Data generation
var total = 0;
for (var i = 0; i < 35; i++) {
  total += 10 * Math.floor(Math.random() + 0.5);
  total += 5 * Math.floor(Math.random() + 0.5);
  total += 5 * Math.floor(Math.random() + 0.5);

  var p = new DataPoint(i + 1, total);
  XPData.data.push(p);
}


// XP Trend Data     
var XPTrendData = {
  label: 'XP Trend',
  fill: false,
  pointRadius: 0,
  lineTension: 0,
  borderDash: [5, 5],
  borderColor: "#ccc",
  backgroundColor: "rgba(0,0,0,0)",
  data: [],
};

// XP Trend calculaion
var total = 0;
var days_so_far = XPData.data.length;
total = XPData.data[days_so_far - 1].y;
var average_per_day = total / days_so_far;

var trend_total = total;
for (i = days_so_far; i < days; i++) {

  p = new DataPoint(i, trend_total);
  XPTrendData.data.push(p);
  trend_total += average_per_day;

}

// Chillax Line Data Set
var ChillaxLineData = {
  label: 'Chillax Line',
  pointRadius: 0,
  backgroundColor: "rgba(0,0,0,0)",
  borderColor: "#337AB7",
  data: [],
};

// Chill Line Generation
for (i = 1; i < days; i++) {
  p = new DataPoint(i, Math.floor(i * chillax * 10 / days));
  ChillaxLineData.data.push(p);

}

var options = {
  scaleUse2Y: true,

  scales: {

    xAxes: [{
      type: 'linear',
      position: 'bottom',
      ticks: {
        max: days,
        min: 0,
        stepSize: 5,
      },
      scaleLabel: {
        display: true,
        labelString: 'Days of Class'
      },
    }],

    yAxes: [{
        id: "y-axis-XP",
        position: 'right',
        ticks: {
          max: 1000,
          min: 0,
          stepSize: 50,
        },

        scaleLabel: {
          display: true,
          labelString: 'XP'
        },

        gridLines: {},
      },
      {
        id: "y-axis-percent",
        position: 'left',

        ticks: {
          max: 100,
          min: 0,
          stepSize: 5,
        },

        scaleLabel: {
          display: true,
          labelString: 'Percent'
        },

        gridLines: {
          /*show: true,
          color: "rgba(255, 255, 255, 1)",
          lineWidth: 1,
          drawOnChartArea: true,
          drawTicks: true,
          zeroLineWidth: 1,
          zeroLineColor: "rgba(255,255,255,1)", */
        },
      }


    ],
  },

  title: {
    text: 'A Map of Your Progress',
    display: true,
  },

  legend: {
    position: 'top',
  },

};

var data = {
  datasets: [XPData,
    XPTrendData,
    ChillaxLineData,
  ]
};

var myChart = new Chart(ctx, {
  type: 'line',
  data: data,
  options: options,
});
<script src="https://cdnjs.cloudflare.com/ajax/libs/Chart.js/2.1.6/Chart.bundle.min.js"></script>
<canvas id="myChart" width="400" height="250"></canvas>

最佳答案

使用当前库

<script src="https://cdnjs.cloudflare.com/ajax/libs/Chart.js/2.5.0/Chart.js"></script>

添加放置

legend: {
    labels: {
      usePointStyle: true,
    },
  }

内部选项而不是内部var XPData = {...}

fiddle

var ctx = document.getElementById("myChart");

class DataPoint {
  constructor(x, y) {
    this.x = x;
    this.y = y;
  }
}

var days = 85;
var chillax = 72.5;

// XP DATA SET 
var XPData = {
  label: 'XP Earned',
  fill: false,
  backgroundColor: "#444",
  borderColor: "#444",
  pointBorderColor: "#444",
  pointBackgroundColor: "#444",
  data: [],
  showLine: false,
};

// XP Data generation
var total = 0;
for (var i = 0; i < 35; i++) {
  total += 10 * Math.floor(Math.random() + 0.5);
  total += 5 * Math.floor(Math.random() + 0.5);
  total += 5 * Math.floor(Math.random() + 0.5);

  var p = new DataPoint(i + 1, total);
  XPData.data.push(p);
}


// XP Trend Data     
var XPTrendData = {
  label: 'XP Trend',
  fill: false,
  pointRadius: 0,
  lineTension: 0,
  borderDash: [5, 5],
  borderColor: "#ccc",
  backgroundColor: "rgba(0,0,0,0)",
  data: [],
};

// XP Trend calculaion
var total = 0;
var days_so_far = XPData.data.length;
total = XPData.data[days_so_far - 1].y;
var average_per_day = total / days_so_far;

var trend_total = total;
for (i = days_so_far; i < days; i++) {

  p = new DataPoint(i, trend_total);
  XPTrendData.data.push(p);
  trend_total += average_per_day;

}

// Chillax Line Data Set
var ChillaxLineData = {
  label: 'Chillax Line',
  pointRadius: 0,
  backgroundColor: "rgba(0,0,0,0)",
  borderColor: "#337AB7",
  data: [],
};

// Chill Line Generation
for (i = 1; i < days; i++) {
  p = new DataPoint(i, Math.floor(i * chillax * 10 / days));
  ChillaxLineData.data.push(p);

}



var options = {
  scaleUse2Y: true,

  scales: {

    xAxes: [{
      type: 'linear',
      position: 'bottom',
      ticks: {
        max: days,
        min: 0,
        stepSize: 5,
      },
      scaleLabel: {
        display: true,
        labelString: 'Days of Class'
      },
    }],

    yAxes: [{
        id: "y-axis-XP",
        position: 'right',
        ticks: {
          max: 1000,
          min: 0,
          stepSize: 50,
        },

        scaleLabel: {
          display: true,
          labelString: 'XP'
        },

        gridLines: {},
      }, {
        id: "y-axis-percent",
        position: 'left',

        ticks: {
          max: 100,
          min: 0,
          stepSize: 5,
        },

        scaleLabel: {
          display: true,
          labelString: 'Percent'
        },

        gridLines: {
          /*show: true,
          color: "rgba(255, 255, 255, 1)",
          lineWidth: 1,
          drawOnChartArea: true,
          drawTicks: true,
          zeroLineWidth: 1,
          zeroLineColor: "rgba(255,255,255,1)", */
        },
      }


    ],
  },

  title: {
    text: 'A Map of Your Progress',
    display: true,
  },
  legend: {
    labels: {
      usePointStyle: true,
    },
  }

};

var data = {
  datasets: [XPData,
    XPTrendData,
    ChillaxLineData,
  ]
};

var myChart = new Chart(ctx, {
  type: 'line',
  data: data,
  options: options,
});
<script src="https://cdnjs.cloudflare.com/ajax/libs/Chart.js/2.5.0/Chart.js"></script>

<canvas id="myChart" width="400" height="250"></canvas>

关于javascript - 在 chart.js 中的图例上显示点样式,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38272120/

相关文章:

javascript - 防止触发 onclick

javascript - 将 javascript 生成的框架转换为 div

全屏 HTML5 中的 javascript 打开(页面元素)在 Internet Explorer 中失败

javascript - jQuery 在最后一个元素之后插入

c++ - QCustomPlot:如何在 y 轴上设置百分比?如何更改刻度标签对齐方式?

c# - 如何过滤 dxChart 数据

javascript - 如何根据您传递给它的数据重新绘制 Google 图表?

javascript - Google Visualization - vAxis maxValue 属性不起作用

javascript - Chart.js 图例不适用于饼图

javascript - Chart js v2 工具提示回调换行符