javascript - 如何在 react-google-charts 中绘制与主要刻度、刻度长度、位置、颜色等刻度相关的样式

标签 javascript reactjs charts google-visualization react-google-charts

le

这张图片可以帮助理解我的问题。需要一个橙色的勾号。

最佳答案

在用来绘制图表的数据表中,
我们可以使用具有注释作用的隐藏系列。

data.addColumn('string', 'x');
data.addColumn('number', 'y');
data.addColumn('number', 'hidden series');
data.addColumn({type:'string', role:'annotation'});

我们对隐藏序列使用零值。
这会将注释放置在 x 轴上。
然后我们给注解一个值,比如'A',这样注解就被绘制出来了。

绘制橙色刻度,并隐藏注释值,
我们使用以下注释选项...

annotations: {
  boxStyle: {
    fill: '#f57c00',
    stroke: '#f57c00',
    strokeWidth: 1
  },
  stem: {
    length: 0
  },
  textStyle: {
    color: 'transparent',
    fontSize: 16
  },
},

您可以使用fontSize 来改变注释的大小。

防止隐藏的系列被绘制,
我们使用以下选项...

series: {
  1: {
    color: 'transparent',
    visibleInLegend: false,
    enableInteractivity: false
  }
},

请参阅以下工作片段...

google.charts.load('current', {
  packages: ['corechart']
}).then(function () {
  var data = new google.visualization.DataTable();
  data.addColumn('string', 'x');
  data.addColumn('number', 'y');
  data.addColumn('number', 'hidden series');
  data.addColumn({type:'string', role:'annotation'});
  data.addRows([
    ['2010-01-01', 10, 0, 'A'],
    ['2012-01-01', 4, 0, 'A'],
    ['2014-01-01', 26, 0, 'A'],
    ['2016-01-01', 50, 0, 'A'],
    ['2018-01-01', 75, 0, 'A']
  ]);

  var options = {
    annotations: {
      boxStyle: {
        fill: '#f57c00',
        stroke: '#f57c00',
        strokeWidth: 1
      },
      stem: {
        length: 0
      },
      textStyle: {
        color: 'transparent',
        fontSize: 12
      },
    },
    chartArea: {
      left: 80,
      top: 56,
      right: 32,
      bottom: 80,
      height: '100%',
      width: '100%'
    },
    hAxis: {
      title: 'Date'
    },
    height: '100%',
    legend: {
      position: 'none'
    },
    series: {
      1: {
        color: 'transparent',
        visibleInLegend: false,
        enableInteractivity: false
      }
    },
    title: 'value vs. Date',
    vAxis: {
      title: 'value'
    },
    width: '100%'
  };

  var chart = new google.visualization.LineChart(document.getElementById('chart'));

  window.addEventListener('resize', function () {
    chart.draw(data, options);
  });
  chart.draw(data, options);
});
html, body {
  height: 100%;
  margin: 0px 0px 0px 0px;
  padding: 0px 0px 0px 0px;
}

#chart {
  height: 100%;
  min-height: 400px;
}
<script src="https://www.gstatic.com/charts/loader.js"></script>
<div id="chart"></div>


编辑

另一种选择是在每个刻度位置绘制方形点,
正如@dlaliberte 所建议的

请参阅以下工作片段...

google.charts.load('current', {
  packages: ['corechart']
}).then(function () {
  var data = new google.visualization.DataTable();
  data.addColumn('string', 'x');
  data.addColumn('number', 'y');
  data.addColumn('number', 'tick series');
  data.addRows([
    ['2010-01-01', 10, 0],
    ['2012-01-01', 4, 0],
    ['2014-01-01', 26, 0],
    ['2016-01-01', 50, 0],
    ['2018-01-01', 75, 0]
  ]);

  var options = {
    chartArea: {
      left: 80,
      top: 56,
      right: 32,
      bottom: 80,
      height: '100%',
      width: '100%'
    },
    hAxis: {
      title: 'Date'
    },
    height: '100%',
    legend: {
      position: 'none'
    },
    series: {
      1: {
        color: '#f57c00',
        lineWidth: 0,
        pointSize: 12,
        pointShape: 'square',
        visibleInLegend: false,
        enableInteractivity: false
      }
    },
    title: 'value vs. Date',
    vAxis: {
      title: 'value'
    },
    width: '100%'
  };

  var chart = new google.visualization.LineChart(document.getElementById('chart'));

  window.addEventListener('resize', function () {
    chart.draw(data, options);
  });
  chart.draw(data, options);
});
html, body {
  height: 100%;
  margin: 0px 0px 0px 0px;
  overflow: hidden;
  padding: 0px 0px 0px 0px;
}

#chart {
  height: 100%;
  min-height: 400px;
}
<script src="https://www.gstatic.com/charts/loader.js"></script>
<div id="chart"></div>

关于javascript - 如何在 react-google-charts 中绘制与主要刻度、刻度长度、位置、颜色等刻度相关的样式,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/63897692/

相关文章:

javascript - 有没有办法在 ruby​​ 中的上传文件或文本区域中找出二进制代码的存在?

javascript - 伪元素的 CSS 动画在 FF 中不起作用

javascript - 当用户点击它时转到下一个 div 持有者?

javascript - 带有用户输入和验证的垂直条形图

ios - 图表中选定切片的颜色

javascript - 使用 Node js 创建 Mongodb ObjectId 时如何捕获错误

reactjs - 使用 TypeScript : why is the type for the state lost when useState with a callback as the updater 使用react

javascript - ReactJS 用于编辑评论

javascript - 使用项目组中的 React Native State 管理选定的项目样式

javascript - 将 JS 图表库与 NodeJS 结合使用