Radial Gradient

Radial Gradient - 图1

createRadialGradient3 config data setup

  1. function createRadialGradient3(context, c1, c2, c3) {
  2. const chartArea = context.chart.chartArea;
  3. if (!chartArea) {
  4. // This case happens on initial chart load
  5. return null;
  6. }
  7. const chartWidth = chartArea.right - chartArea.left;
  8. const chartHeight = chartArea.bottom - chartArea.top;
  9. if (width !== chartWidth || height !== chartHeight) {
  10. cache.clear();
  11. }
  12. var gradient = cache.get(c1 + c2 + c3);
  13. if (!gradient) {
  14. // Create the gradient because this is either the first render
  15. // or the size of the chart has changed
  16. width = chartWidth;
  17. height = chartHeight;
  18. const centerX = (chartArea.left + chartArea.right) / 2;
  19. const centerY = (chartArea.top + chartArea.bottom) / 2;
  20. const r = Math.min(
  21. (chartArea.right - chartArea.left) / 2,
  22. (chartArea.bottom - chartArea.top) / 2
  23. );
  24. var ctx = context.chart.ctx;
  25. gradient = ctx.createRadialGradient(centerX, centerY, 0, centerX, centerY, r);
  26. gradient.addColorStop(0, c1);
  27. gradient.addColorStop(0.5, c2);
  28. gradient.addColorStop(1, c3);
  29. cache.set(c1 + c2 + c3, gradient);
  30. }
  31. return gradient;
  32. }
  1. const config = {
  2. type: 'polarArea',
  3. data: data,
  4. options: {
  5. plugins: {
  6. legend: false,
  7. tooltip: false,
  8. },
  9. elements: {
  10. arc: {
  11. backgroundColor: function(context) {
  12. let c = colors[context.dataIndex];
  13. if (!c) {
  14. return;
  15. }
  16. if (context.active) {
  17. c = helpers.getHoverColor(c);
  18. }
  19. const mid = helpers.color(c).desaturate(0.2).darken(0.2).rgbString();
  20. const start = helpers.color(c).lighten(0.2).rotate(270).rgbString();
  21. const end = helpers.color(c).lighten(0.1).rgbString();
  22. return createRadialGradient3(context, start, mid, end);
  23. },
  24. }
  25. }
  26. }
  27. };
  1. function generateData() {
  2. return Utils.numbers({
  3. count: DATA_COUNT,
  4. min: 0,
  5. max: 100
  6. });
  7. }
  8. const data = {
  9. labels: Utils.months({count: DATA_COUNT}),
  10. datasets: [{
  11. data: generateData()
  12. }]
  13. };
  1. const DATA_COUNT = 5;
  2. Utils.srand(110);
  3. const chartColors = Utils.CHART_COLORS;
  4. const colors = [chartColors.red, chartColors.orange, chartColors.yellow, chartColors.green, chartColors.blue];
  5. const cache = new Map();
  6. let width = null;
  7. let height = null;
  8. const actions = [
  9. {
  10. name: 'Randomize',
  11. handler(chart) {
  12. chart.data.datasets.forEach(dataset => {
  13. dataset.data = generateData();
  14. });
  15. chart.update();
  16. }
  17. },
  18. ];