我需要在我的bar图表上添加一条简单的点/垂直线,它有一个动态的X值,0代表Y值.预览我需要的内容(红点):

enter image description here

绿色值是动态的.

预览我的当前状态:

enter image description here

Where 3.30应该是点的X坐标-[3.30,0].

我用Vue chart表示图表,我试着用barscatter创建一个mixed,但scatter需要type: 'linear',因为它是xAxis,这不适合我对bar图表的需要.

所以我try 了chartjs-plugin-annotation,它是box类型,接受"坐标",但这里的问题是Xmust be a fixed value on the X axis(labels对象).如果我把X轴[3,0]放进go ,它会起作用,但是如果有一个十进制数,比如[3.5,0],它就不会起作用.


  // data
  options: {
    responsive: true,
    maintainAspectRatio: false,
    legend: {
      display: false
    },
    scales: {
      yAxes: [{
        ticks: {
          min: 0,
          max: 1,
          stepSize: 0.1
        }
      }]
    }
  }

  // computed 
  labels: [1, 2, 3, 4, 5, 6], // fixed value, there are always 6 bars
  datasets: [
    {
      label: 'Values',
      backgroundColor: '#f89098',
      data: this.tableInputValues // array of decimal values
    }
  ]

So, my question is how to put a "simple" dot, or a vertical line, on a Chart.js bar chart where the dot has a dynamical value for the X axis -> [dynamic value, 0].

仅供参考——大约Expected value美元

推荐答案

据我所知,Vue图表使用画布(如图Demo page所示)

var c = document.getElementById("bar-chart");   //hereby assuming canvas named "bar-chart"
var ctx = c.getContext("2d");
ctx.fillStyle = "#ff0000";                     //red color for the dot
ctx.beginPath();
let yPosition = c.height - 5;                 //fixed y position
let xPosition = 35;                          //that's the dynamic expected value
ctx.arc(xPosition, yPosition, 2.5, 0, 2 * Math.PI);
ctx.fill();

Here你会发现一个演示如何使用Vue实现这一点.在这个场景中,需要将代码包装起来,在画布上用afterDraw钩子画一个点.这个钩子需要作为插件连接到图表组件,如下所示:

...
mounted () { 
   //adding the plugin to draw the red dot
   this.addPlugin({
    id: 'chart-plugin',
    afterDraw: function (chart) {
       var c = chart.canvas;   
       var ctx = c.getContext("2d");
       ctx.fillStyle = "#ff0000";                     
       ctx.beginPath();
       let xPosition = 742; //x positioning to be calculated according to your needs
       let yPosition = c.height - 28;                                       
       ctx.arc(xPosition, yPosition, 3, 0, 2 * Math.PI);
       ctx.fill();
    }
  });

  //actual chart rendering
  this.renderChart({ 
    ...
  });
}
...

为了完整起见,你可以找到图表中所有可用钩子的列表.js插件API.

Vue.js相关问答推荐

如何将 cogo toast PRIVE集成到nuxt3中?

为什么我的数组在 onBeforeMount 函数中不为空,但在 onMounted 函数中为空

Vuetify/Vue3 插槽模板之间的阴影

过渡如何在 Vue JS 3 中的图像上工作?

Vue3如何将自定义事件绑定到路由视图中的特定组件?

如何在 VUE.js 中重新刷新时保持多层 b-collapse 菜单打开?

如何在vue中获取特定类别的产品列表

在 VueJS 中为 Get、Post、Patch 配置全局标头的最佳方法

如何使用带有自定义标签和类的`transition-group`

Vuetify Datatable - 在所有列上启用内容编辑

在 vuejs 中的图像中包含 router-link 标记

在 SASS 中使用 Vuetify 类

如何根据生产或开发模式读取不同的 .env 文件?

Vuex - 如何跨不同选项卡持久存储更新?

Vue更改宽度和内容

如何从 vue-apollo 中访问 this.$route?

输入文件 Select 事件在 Select 同一文件时未触发

vue 未在实例上定义,但在渲染期间引用

IOS在输入焦点上显示键盘

Vue Router beforeRouteLeave 不会停止子组件