Google Charts LineChart Custom Points

后端 未结 3 1981
悲哀的现实
悲哀的现实 2021-01-25 02:00

Is it possible to add a custom point shape to a line chart?

Google\'s Customizing Points Documentation doesn\'t mention anything about adding shapes the

相关标签:
3条回答
  • 2021-01-25 02:29

    Basically: when you want to apply a custom style you must use a style-column, the style will be set via the value of the column.

    Example using the object-literal:

    google.setOnLoadCallback(drawChart);
    
    function drawChart() {
      var chartData = {
    
        data: {
    
          cols: [{
    
              label: "X",
              type: "number"
            }, {
              label: 'Y',
              type: "number"
            }, {
              role: 'style',
              type: "string"
            }
    
          ],
          rows: [{
            c: [{
              v: 1
            }, {
              v: 5
            }, {
              v: 'point { stroke-width: 4; fill-color: transparent; stroke-color: red;}'
            }]
          }, {
            c: [{
              v: 2
            }, {
              v: 1
            }, {
              v: 'point { stroke-width: 4; fill-color: transparent; stroke-color: red;}'
            }]
          }, {
            c: [{
              v: 3
            }, {
              v: 3
            }, {
              v: 'point { stroke-width: 4; fill-color: transparent; stroke-color: red;}'
            }]
          }]
        }
      };
      
      var options = {
        legend: 'none',
        curveType: 'function',
        pointSize: 7
    
      };
    
      var chart = new google.visualization.LineChart(document.getElementById('chart_div'));
      chart.draw(new google.visualization.DataTable(chartData.data), options);
    }
    <script type="text/javascript" src="https://www.google.com/jsapi?autoload={'modules':[{'name':'visualization','version':'1.1','packages':['corechart']}]}"></script>
    <div id="chart_div"></div>

    If you're using multiple vaxes then add the column with role: 'style' immediately after the column you want to style.

    Another solution:

    set the style of the circles via CSS:

    google.setOnLoadCallback(drawChart);
    
    function drawChart() {
      var chartData = {
    
        data: {
    
          cols: [{
    
              label: "X",
              type: "number"
            }, {
              label: 'Y',
              type: "number"
            }, {
              label: 'Y',
              type: "number"
            }
    
          ],
          rows: [
                 {c:[{v:1}, {v:5}, {v:4}]},
                 {c:[{v:2}, {v:1}, {v:2}]},
                 {c:[{v:3}, {v:3}, {v:5}]}
                ]
        }
      };
    
    
      var options = {
        curveType: 'function',
        pointSize: 7,
        series: {
          //set unique colors for the series when you must set
          //different points for mmultiple series
          0: {
            color: 'ff0000'
          },
          1: {
            color: '#0000ff'
          }
        }
    
      };
    
      var chart = new google.visualization.LineChart(document.getElementById('chart_div'));
      chart.draw(new google.visualization.DataTable(chartData.data), options);
    }
    #chart_div circle {
      stroke-width: 4px !important;
      fill: none !important;
    }
    #chart_div circle[fill="#ff0000"] {
      stroke: #ff0000 !important;
    }
    #chart_div circle[fill="#0000ff"] {
      stroke: #0000ff !important;
    }
    <script type="text/javascript" src="https://www.google.com/jsapi?autoload={'modules':[{'name':'visualization','version':'1.1','packages':['corechart']}]}"></script>
    <div id="chart_div" ></div>

    0 讨论(0)
  • 2021-01-25 02:48

    For whoever is using Dr.Molle's brilliant CSS solution and is experiencing an issue where the second data series points disappear just make the second color the default color:

    #chart_div circle {
      stroke-width: 4px !important;
      fill: #ffffff !important; // note that i'm not using transparent
      stroke: #0000ff !important; // second color moved to be default
    }
    #chart_div circle[fill="#ff0000"] {
      stroke: #ff0000 !important;
    }
    #chart_div circle[fill="#0000ff"] {
      /*stroke: #0000ff !important;*/
    }
    

    **note: couldn't write this as a comment to Dr.Molle's answer due to not having enough reputation at the time.

    0 讨论(0)
  • 2021-01-25 02:55

    Dr. Molle's answer seemed to work for getting a hollow circle, although the point doesn't match the legend.

    I figured out a workaround for an X shapped point. I used options.pointshape to give a star 4 sides, rotated it by 45 degrees and decreased the dent:

    pointShape: { 
       type: 'star',
       sides: 4,
       dent: 0.1,
       rotation: 45,
    },
    

    Hopefully there is a better way, but Dr. Molle's and this answer work for now.

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