Google Chart Tooltip Not Working

前端 未结 3 735
予麋鹿
予麋鹿 2021-01-14 09:24

I am currently working on Google Chart using ASP.NET and connecting it to the database (SQL Server). But I have a problem when trying to customize the tool tip.

Here

3条回答
  •  感情败类
    2021-01-14 10:09

    unfortunately, Column Roles, including tooltips, don't work with Material charts, only Core

    Material --> packages: ['bar'] + google.charts.Bar

    Core --> packages: ['corechart'] + google.visualization.BarChart

    you can use the following configuration option to get Core close to the look & feel of Material

    theme: 'material'

    note 1: when using a tooltip column, all of the tooltip content must be provided, it doesn't append anything to the default tooltip

    see following working snippet...

    google.charts.load('current', {
      callback: function () {
        // simulate data
        dataValues = [
          {
            customer: 'Customer A',
            percent_submitted: 10
          },
          {
            customer: 'Customer B',
            percent_submitted: 20
          },
          {
            customer: 'Customer C',
            percent_submitted: 30
          },
        ];
    
        drawchart(dataValues);
      },
      packages: ['corechart']
    });
    
    function drawchart(dataValues) {
        // Callback that creates and populates a data table,
        // instantiates the pie chart, passes in the data and
        // draws it.
    
        // Instantiate and draw our chart, passing in some options
        var chart = new google.visualization.BarChart(document.getElementById('BarChartsDIV'));
        var data = new google.visualization.DataTable();
    
        data.addColumn('string', 'customer');
        data.addColumn('number', 'percent_submitted')
        data.addColumn({type: 'string', role: 'tooltip'});
    
    
        for (var i = 0; i < dataValues.length; i++) {
            data.addRow([dataValues[i].customer,
            { v: dataValues[i].percent_submitted, f: dataValues[i].percent_submitted + '%'},
            dataValues[i].customer + '\nTEST TOOL TIP\n' + dataValues[i].percent_submitted + '%']);
        }
    
        var view = new google.visualization.DataView(data);
        view.setColumns([0, 1, 2]);
    
        chart.draw(view,
          {
              theme: 'material',
              tooltip: { isHtml: true},
              title: "",
              legend: { position: 'none' },
              bars: 'horizontal', // Required for Material Bar Charts.
              axes: {
                  x: {
                      0: { side: 'top', label: '' }, // Top x-axis.
                  },
                  y: {
                      0: { label: '' } //Side y-axis
                  }
    
              },
              bar: { groupWidth: '70%' },
    
          });
    }
    
    

    note 2: for HTML tooltips to work, you must include a column property

    data.addColumn({type: 'string', role: 'tooltip', p: {html: true}});

    see following working snippet...

    google.charts.load('current', {
      callback: function () {
        // simulate data
        dataValues = [
          {
            customer: 'Customer A',
            percent_submitted: 10
          },
          {
            customer: 'Customer B',
            percent_submitted: 20
          },
          {
            customer: 'Customer C',
            percent_submitted: 30
          },
        ];
    
        drawchart(dataValues);
      },
      packages: ['corechart']
    });
    
    function drawchart(dataValues) {
        // Callback that creates and populates a data table,
        // instantiates the pie chart, passes in the data and
        // draws it.
    
        // Instantiate and draw our chart, passing in some options
        var chart = new google.visualization.BarChart(document.getElementById('BarChartsDIV'));
        var data = new google.visualization.DataTable();
    
        data.addColumn('string', 'customer');
        data.addColumn('number', 'percent_submitted')
        // include column property for html tooltips
        data.addColumn({type: 'string', role: 'tooltip', p: {html: true}});
    
    
        for (var i = 0; i < dataValues.length; i++) {
            data.addRow([dataValues[i].customer,
            { v: dataValues[i].percent_submitted, f: dataValues[i].percent_submitted + '%'},
            // need to include own styling as well
            '
    ' + dataValues[i].customer + '
    TEST TOOL TIP
    ' + dataValues[i].percent_submitted + '%
    ']); } var view = new google.visualization.DataView(data); view.setColumns([0, 1, 2]); chart.draw(view, { theme: 'material', tooltip: { isHtml: true}, title: "", legend: { position: 'none' }, bars: 'horizontal', // Required for Material Bar Charts. axes: { x: { 0: { side: 'top', label: '' }, // Top x-axis. }, y: { 0: { label: '' } //Side y-axis } }, bar: { groupWidth: '70%' }, }); }
    .ggl-tooltip {
      border: 1px solid #E0E0E0;
      font-family: Arial, Helvetica;
      font-size: 12pt;
      padding: 12px 12px 12px 12px;
    }
    
    .ggl-tooltip div {
      padding-top: 6px;
    }
    
    

    note 3: as for Material charts, they show the formatted value (f:) by default, so you could add a bit of text there, or at the end of the column headings, which would be for all rows

    see following working snippet...

    google.charts.load('current', {
      callback: function () {
        // simulate data
        dataValues = [
          {
            customer: 'Customer A',
            percent_submitted: 10
          },
          {
            customer: 'Customer B',
            percent_submitted: 20
          },
          {
            customer: 'Customer C',
            percent_submitted: 30
          },
        ];
    
        drawchart(dataValues);
      },
      packages: ['bar']
    });
    
    function drawchart(dataValues) {
        // Callback that creates and populates a data table,
        // instantiates the pie chart, passes in the data and
        // draws it.
    
        // Instantiate and draw our chart, passing in some options
        var chart = new google.charts.Bar(document.getElementById('BarChartsDIV'));
        var data = new google.visualization.DataTable();
    
        data.addColumn('string', 'customer');
        data.addColumn('number', 'percent_submitted \n OTHER TOOLTIP TEXT FOR ALL ROWS')
    
        for (var i = 0; i < dataValues.length; i++) {
            data.addRow([dataValues[i].customer,
            { v: dataValues[i].percent_submitted, f: dataValues[i].percent_submitted + '% TEST TOOL TIP'}]);
        }
    
        var view = new google.visualization.DataView(data);
        view.setColumns([0, 1]);
    
        chart.draw(view,
          {
              tooltip: { isHtml: true},
              title: "",
              legend: { position: 'none' },
              bars: 'horizontal', // Required for Material Bar Charts.
              axes: {
                  x: {
                      0: { side: 'top', label: '' }, // Top x-axis.
                  },
                  y: {
                      0: { label: '' } //Side y-axis
                  }
    
              },
              bar: { groupWidth: '70%' },
    
          });
    }
    
    

    note 4: although not recommended, it is possible to modify the tooltip manually via SVG DOM, when the chart's 'ready' event fires...

提交回复
热议问题