You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何在图表Marker中展示除坐标点(x,y)外的自定义数据?

Customizing Marker Labels to Show Raw Calculation Data

Got it, let's tackle this! Since you already know how to tweak marker text and x/y coordinates, the main trick here is to store the raw reps and weight data right alongside your calculated y-value, then pull that data when rendering the marker label instead of relying on the default x/y output.

Here's a step-by-step breakdown using common charting libraries (the core logic applies to most tools):

1. Attach Raw Data to Your Chart Dataset

First, make sure each data point includes the reps and weight values used to calculate the y-value (233.3 in your example). Don't just store the computed y—keep the source data tied to the point:

// Example for Chart.js
const workoutData = {
  labels: ['Session 1'], // X-axis labels
  datasets: [{
    label: 'Calculated Strength',
    data: [
      {
        x: 'Session 1',
        y: 233.3,
        reps: 5, // Raw data we want to display
        weight: 200 // Raw data we want to display
      }
    ],
    // Add your usual styling (color, point radius, etc.)
  }]
};

2. Override the Marker/Tooltip Label Renderer

Next, tell your chart library to use the raw reps/weight instead of the default x/y values for the marker label. The exact method varies by library, but here are two common examples:

For Chart.js (Tooltip or Inline Marker Text)

If you want the label in the tooltip that pops up on hover:

const chartOptions = {
  plugins: {
    tooltip: {
      callbacks: {
        label: (context) => {
          // Pull the raw data from the current point
          const { reps, weight } = context.parsed;
          return `${reps} reps @ ${weight} lb`;
        }
      }
    }
  },
  // If you want text directly on the marker (not just tooltip):
  elements: {
    point: {
      label: {
        display: true,
        formatter: (context) => {
          const { reps, weight } = context.dataset.data[context.dataIndex];
          return `${reps}@${weight}`; // Shortened for space, adjust as needed
        }
      }
    }
  }
};

For Highcharts

Highcharts uses a similar approach with a tooltip formatter:

const chartOptions = {
  tooltip: {
    formatter: function() {
      return `${this.point.reps} reps @ ${this.point.weight} lb`;
    }
  },
  series: [{
    data: [
      {
        y: 233.3,
        reps: 5,
        weight: 200
      }
    ]
  }]
};

3. Key Takeaway

No matter which library you're using, the core idea is the same: bind your raw calculation data to each data point so you can reference it when customizing the marker label. This replaces the default x/y output with the exact values you used to compute the y-value.

内容的提问来源于stack exchange,提问作者Jim

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.25 04:25:35