ChartJS用于将Point定位到条形图(范围)的搜索图表类型

b4lqfgs4  于 2023-01-26  发布在  Chart.js
关注(0)|答案(1)|浏览(223)

我正在尝试将excel制作的这个图表迁移到chartJS中:

重要特性:

  • 具有显示范围的水平条
  • 假设范围从"下限"(0%)到"上限"(100%)
  • 中位数用垂直线表示(50%)
  • 在范围的某处放置一个单点(例如,23%)
  • 这里只有一个点是动态的,其他的看起来都一样

我知道这不是一个典型的图表,有点特殊。
我找到的最接近的图表是:(一)

  • 一个简单的堆叠条形图取代了明亮明显的星星,我只是另一个条形堆叠

  • 不是很漂亮一样的

  • 好坏参半的图表

  • 使用注解插件画一条线(这里我用画图工具手工画线)

  • 在css中,整个图表需要旋转90 °(已在下图中完成)

另一个选择是使用plane css的东西来创建它,但是我更愿意使用chart js,因为有更多的图表,我所有的框架都是为它设计的。
任何建议都很感谢。谢谢。

taor4pac

taor4pac1#

charts.js允许您使用简单的机制访问画布并绘制自定义内容。
我们可以从条形图开始,比如一个水平条形图,其中一个项目和大多数其他项目都被禁用:

  1. const data = {
  2. labels: [""],
  3. datasets: [{
  4. label: '100%',
  5. data: [100],
  6. backgroundColor: '#4af',
  7. barThickness: 100
  8. }]
  9. };
  10. const options = {
  11. type: 'bar',
  12. data,
  13. options: {
  14. animation: {duration: 0},
  15. indexAxis: 'y',
  16. layout: {
  17. padding:{
  18. left: 10,
  19. right: 10
  20. }
  21. },
  22. scales: {
  23. x: {
  24. display: false
  25. },
  26. y: {
  27. display: false
  28. }
  29. },
  30. plugins: {
  31. legend: {
  32. display: false
  33. },
  34. tooltip:{
  35. enabled: false
  36. }
  37. }
  38. }
  39. };
  40. const chart = new Chart(document.getElementById("myChart"), options);
  1. <script src="https://cdnjs.cloudflare.com/ajax/libs/Chart.js/4.2.0/chart.umd.min.js" crossorigin="anonymous" referrerpolicy="no-referrer"></script>
  2. <body>
  3. <canvas id="myChart" style="height:250px; width: 90vw; border: 1px solid #ddd "></canvas>
  4. </body>

现在我们可以通过插件访问画布,绘制所有其他内容:

  1. const plugin = {
  2. id: 'customDraw', // to identify the plugin in the chart options
  3. afterDraw: (chart, args, options) => {
  4. const {ctx} = chart;
  5. // read plugin options
  6. const lineWidth = options.lineWidth || 1,
  7. lineColor = options.lineColor || '#000',
  8. textColor = options.textColor || '#000',
  9. textFont = options.textFont,
  10. starAt = options.starAt,
  11. starColor = options.starColor || '#f44';
  12. // get pixel coordinates for our bar, that is
  13. // positioned at y = 0, from x = 0 to x = 100
  14. const yCenter = chart.scales.y.getPixelForValue(0),
  15. yTop = yCenter-50,
  16. yBottom = yCenter+50,
  17. x0 = chart.scales.x.getPixelForValue(0),
  18. x50 = chart.scales.x.getPixelForValue(50),
  19. x100 = chart.scales.x.getPixelForValue(100),
  20. xStar = chart.scales.x.getPixelForValue(starAt);
  21. ctx.save();
  22. ctx.strokeStyle = lineColor;
  23. ctx.lineWidth = lineWidth;
  24. ctx.beginPath();
  25. ctx.moveTo(x50, yTop);
  26. ctx.lineTo(x50, yBottom);
  27. ctx.stroke();
  28. ctx.fillStyle = starColor;
  29. drawStar(ctx, xStar, yCenter, 10);
  30. ctx.textBaseline = "top";
  31. ctx.fillStyle = textColor;
  32. if(textFont){
  33. ctx.font = textFont;
  34. }
  35. ctx.textAlign = "start";
  36. ctx.fillText("Lower", x0, yBottom + 2);
  37. ctx.textAlign = "center";
  38. ctx.fillText("Median", x50, yBottom + 2);
  39. ctx.textAlign = "right";
  40. ctx.fillText("Upper", x100, yBottom + 2);
  41. ctx.restore();
  42. }
  43. };

完整代码:
一个三个三个一个
下一步将是添加自定义交互-工具提示,点击事件和一切...

展开查看全部

相关问题