获取图表.js 4中柱形图的X坐标

huangapple go评论63阅读模式
英文:

Get X-coordinates for bars in chart.js 4

问题

I use Chart.js v4.2.1

<html>
<head>
    <meta charset="utf-8" />
    <title>Bar chart</title>
    <script type="text/javascript" src="https://cdn.jsdelivr.net/npm/chart.js"></script>
</head>
<body>
    <div>
        <canvas id="barchart"></canvas>
    </div>
</body>
<script type="text/javascript">
    var canvas = document.getElementById('barchart');
    var chart = new Chart(canvas,
    {
        type: 'bar',
        data:
        {
            labels: ["Audi", "VW", "KIA"],
            datasets:
            [
              {
                  label: "Cars",
                  backgroundColor: ["#3e95cd", "#8e5ea2", "#3cba9f"],
                  data: [2601, 4769, 602],
               },
            ],
        },
    });
</script>
</html>

To get number of bars I execute chart.data.datasets[0].data.length and get 3.
To get the Y-value for the first bar I do chart.data.datasets[0].data[0] and get 2601.

How do I get the X-values (X-coordinates) for the bars?
(I am not interested in using any plugin).

Added:
Here is a sample where chart.scales.x is defined but chart.scales.y is not.
This happen when I add yAxisID which I need in my complete work.

<html>
<head>
    <meta charset="utf-8" />
    <title>Bar chart</title>
    <script type="text/javascript" src="https://cdn.jsdelivr.net/npm/chart.js"></script>
</head>
<body>
    <div>
        <canvas id="barchart"></canvas>
        <div id="debug"></div>
    </div>
<script type="text/javascript">
    var canvas = document.getElementById('barchart');
    var chart = new Chart(canvas,
    {
        type: 'bar',
        data:
        {
            labels: ["Audi", "VW", "KIA"],
            datasets:
            [
                {
                    label: "Cars",
                    backgroundColor: ["#3e95cd", "#8e5ea2", "#3cba9f"],
                    data: [2601, 4769, 602],
                    yAxisID: "cars",
                },
            ],
        },
        options:
        {
            scales:
            {
                cars:
                {
                    position: "left",
                    ticks:
                    {
                        color: "red",
                    },
                    grid:
                    {
                        display: true,
                    },
                },
            },
        },
    });
    var dataSets = chart.data.datasets;
    var xPos = chart.scales.x.getPixelForValue(dataSets[0].data[0]);
    try
    {
        var yPos = chart.scales.cars.getPixelForValue(dataSets[0].data[0]);    // --> here y is undefined
        document.getElementById("debug").innerHTML = "xPos=" + xPos + ", yPos=" + yPos;
    }
    catch(e)
    {
        document.getElementById("debug").innerHTML = "xPos=" + xPos + "<br>" + e;
    }
</script>
</body>
</html>
英文:

I use Chart.js v4.2.1

&lt;html&gt;
&lt;head&gt;
    &lt;meta charset=&quot;utf-8&quot; /&gt;
    &lt;title&gt;Bar chart&lt;/title&gt;
    &lt;script type=&quot;text/javascript&quot; src=&quot;https://cdn.jsdelivr.net/npm/chart.js&quot;&gt;&lt;/script&gt;
&lt;/head&gt;
&lt;body&gt;
    &lt;div&gt;
        &lt;canvas id=&quot;barchart&quot;&gt;&lt;/canvas&gt;
    &lt;/div&gt;
&lt;/body&gt;
&lt;script type=&quot;text/javascript&quot;&gt;
    var canvas = document.getElementById(&#39;barchart&#39;);
    var chart = new Chart(canvas,
    {
        type: &#39;bar&#39;,
        data:
        {
            labels: [&quot;Audi&quot;, &quot;VW&quot;, &quot;KIA&quot;],
            datasets:
            [
              {
                  label: &quot;Cars&quot;,
                  backgroundColor: [&quot;#3e95cd&quot;, &quot;#8e5ea2&quot;, &quot;#3cba9f&quot;],
                  data: [2601, 4769, 602],
               },
            ],
        },
    });
&lt;/script&gt;
&lt;/html&gt;

To get number of bars I execute chart.data.datasets[0].data.length and get 3.
To get the Y-value for the first bar I do chart.data.datasets[0].data[0] and get 2601.

How do I get the X-values (X-coordinates) for the bars?
(I am not interested in using any plugin).

Added:
Here is a sample where chart.scales.x is defined but chart.scales.y is not.
This happen when I add yAxisID which I need in my complete work.

&lt;html&gt;
&lt;head&gt;
    &lt;meta charset=&quot;utf-8&quot; /&gt;
    &lt;title&gt;Bar chart&lt;/title&gt;
    &lt;script type=&quot;text/javascript&quot; src=&quot;https://cdn.jsdelivr.net/npm/chart.js&quot;&gt;&lt;/script&gt;
&lt;/head&gt;
&lt;body&gt;
    &lt;div&gt;
        &lt;canvas id=&quot;barchart&quot;&gt;&lt;/canvas&gt;
        &lt;div id=&quot;debug&quot;&gt;&lt;/div&gt;
    &lt;/div&gt;
&lt;script type=&quot;text/javascript&quot;&gt;
    var canvas = document.getElementById(&#39;barchart&#39;);
    var chart = new Chart(canvas,
    {
        type: &#39;bar&#39;,
        data:
        {
            labels: [&quot;Audi&quot;, &quot;VW&quot;, &quot;KIA&quot;],
            datasets:
            [
                {
                    label: &quot;Cars&quot;,
                    backgroundColor: [&quot;#3e95cd&quot;, &quot;#8e5ea2&quot;, &quot;#3cba9f&quot;],
                    data: [2601, 4769, 602],
                    yAxisID: &quot;cars&quot;,
                },
            ],
        },
        options:
        {
            scales:
            {
                cars:
                {
                    position: &quot;left&quot;,
                    ticks:
                    {
                        color: &quot;red&quot;,
                    },
                    grid:
                    {
                        display: true,
                    },
                },
            },
        },
    });
    var dataSets = chart.data.datasets;
    var xPos = chart.scales.x.getPixelForValue(dataSets[0].data[0]);
    try
    {
        var yPos = chart.scales.cars.getPixelForValue(dataSets[0].data[0]);    // --&gt; here y is undefined
        document.getElementById(&quot;debug&quot;).innerHTML = &quot;xPos=&quot; + xPos + &quot;, yPos=&quot; + yPos;
    }
    catch(e)
    {
        document.getElementById(&quot;debug&quot;).innerHTML = &quot;xPos=&quot; + xPos + &quot;&lt;br&gt;&quot; + e;
    }
&lt;/script&gt;
&lt;/body&gt;
&lt;/html&gt;

答案1

得分: 3

你可以使用以下代码: chartInstance.scales.x.getPixelForValue(chart instance.data.labels[labelIndex]

英文:

You can use the following code: chartInstance.scales.x.getPixelForValue(chart instance.data.labels[labelIndex]

huangapple
  • 本文由 发表于 2023年2月19日 21:32:11
  • 转载请务必保留本文链接:https://go.coder-hub.com/75500481.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定