Chart.js icon indicating copy to clipboard operation
Chart.js copied to clipboard

Configurable axis title location

Open phongpt156 opened this issue 8 years ago • 5 comments

After reading document, i don't found any options to add title to X and Y coordinate like image image See title "Month" and "Percent"

phongpt156 avatar Feb 01 '18 03:02 phongpt156

Tagging as an enhancement because this is not currently possible

etimberg avatar Feb 01 '18 23:02 etimberg

@zedsimple @etimberg I think you can use scaleLabel under yAxes or xAxes to change the label. For example scales: { xAxes: [{ display: true, scaleLabel: { display: true, labelString: 'Month' } }], yAxes: [{ type: 'category', position: 'left', display: true, scaleLabel: { display: true, labelString: 'Request State' }

LordDraagonLive avatar Aug 17 '18 10:08 LordDraagonLive

@phongpt156 Hey I want to submit the solution for this

jatiny17 avatar Oct 14 '19 18:10 jatiny17

@phongpt156 I think this can work

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'
          }
        } ]
      }
    }
  };

jatiny17 avatar Oct 14 '19 19:10 jatiny17

For anyone wanting this, you can use a custom plugin to achieve this:

const customTitle = {
  id: 'customTitle',
  beforeLayout: (chart, args, opts) => {
    if (!opts.x.display) {
      return;
    }

    const {
      ctx
    } = chart;
    ctx.font = opts.x.font || '12px "Helvetica Neue", Helvetica, Arial, sans-serif'

    const {
      width
    } = ctx.measureText(opts.x.text);
    chart.options.layout.padding.right = width * 1.3;
  },
  afterDraw: (chart, args, opts) => {
    const {
      ctx,
      scales: {
        x,
        y
      },
      chartArea: {
        top,
        bottom,
        left,
        right
      }
    } = chart;

    if (opts.x.display) {
      ctx.fillStyle = opts.x.color || Chart.defaults.color
      ctx.font = opts.x.font || '12px "Helvetica Neue", Helvetica, Arial, sans-serif'
      ctx.fillText(opts.x.text, (right + (opts.x.offsetX || 0)), (bottom + ((opts.x.offsetY * -1) || 0)))
    }

    if (opts.y.display) {
      ctx.fillStyle = opts.y.color || Chart.defaults.color
      ctx.font = opts.y.font || '12px "Helvetica Neue", Helvetica, Arial, sans-serif'
      ctx.fillText(opts.y.text, opts.y.offsetX || 3, (top + ((opts.y.offsetY * -1) || -15)))
    }
  }
}

const options = {
  type: 'line',
  data: {
    labels: ["Red", "Blue", "Yellow", "Green", "Purple", "Orange"],
    datasets: [{
        label: '# of Votes',
        data: [12, 19, 3, 5, 2, 3],
        borderWidth: 1
      },
      {
        label: '# of Points',
        data: [7, 11, 5, 8, 3, 7],
        borderWidth: 1
      }
    ]
  },
  options: {
    layout: {
      padding: {
        right: 0 // Seems nesesarry cuz otherwise padding wont update
      }
    },
    plugins: {
      customTitle: {
        y: {
          display: true,
          text: 'Numbers'
        },
        x: {
          display: true,
          text: 'Month',
          offsetX: 5,
          offsetY: 5,
          font: '12px Comic Sans MS'
        }
      }
    }
  },
  plugins: [customTitle]
}

https://jsfiddle.net/Leelenaleee/L2vhwe6u/33/

LeeLenaleee avatar Sep 04 '21 13:09 LeeLenaleee