Google Charts - HTML axis label and title

前端 未结 1 897
旧时难觅i
旧时难觅i 2021-01-22 17:48

So I am trying to make html axis labels in google charts. However there does not seem to be any support for creating the axis labels or the title as HTML objects. The title is e

1条回答
  •  后悔当初
    2021-01-22 18:10

    the labels will only accept text...

    the chart is drawn with svg, which can be changed manually when the 'ready' event fires

    the labels will be elements
    to change the font style inline, use svg elements within

    e.g.

    Xm
    

    see following working snippet...

    google.charts.load('current', {
      callback: function () {
        var data = [['X','Y']], i, options,
            chart, dataTable;
        for (i = 0; i < 20; i += 1) {
          data.push([i, i * i]);
        }
        dataTable = google.visualization.arrayToDataTable(data);
        options = {
          legend: 'none',
          title: 'Xm versus X2',
          hAxis: {title: 'Xm'},
          vAxis: {title: 'X2'}
        };
    
        chart = new google.visualization.ScatterChart(document.getElementById('chart_div'));
    
        google.visualization.events.addListener(chart, 'ready', function () {
          $.each($('text'), function (index, label) {
            var labelText = $(label).text();
            if (labelText.indexOf('X') > -1) {
              labelText = labelText.replace(new RegExp(/m/g), 'm');
              labelText = labelText.replace(new RegExp(/2/g), '2');
              $(label).html(labelText);
            }
          });
        });
    
        chart.draw(dataTable, options);
      },
      packages: ['corechart']
    });
    
    
    

    0 讨论(0)
提交回复
热议问题