如何在Chartjs的线图中为每个Y轴的X轴创建黑色边框

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

How to create Black Border for Every Y Scale's X Axis with Line Chart in Chartjs

问题

以下是您要翻译的内容:

"I want to have black border in every border of X axises of Y scales. How can I do it and have a border that starts with 0 in x axises. So I want to have black border for every x axis and that borders should have value 0. (I just shared y1 and y2, but there is actually y3 and y4 too in my code) Here is my code of my scales:"

我想在Y轴的每个X轴边框上都有黑色边框。我该如何实现,并且在X轴上从0开始设置边框。所以我想要每个X轴都有黑色边框,并且这些边框的值应为0。(我只分享了y1和y2,但我的代码中实际上还有y3和y4)这是我的刻度代码:

英文:

如何在Chartjs的线图中为每个Y轴的X轴创建黑色边框

I want to have black border in every border of X axises of Y scales.
How can I do it and have a border that starts with 0 in x axises.
So I want to have black border for every x axis and that borders should have value 0.
(I just shared y1 and y2, but there is actually y3 and y4 too in my code)
Here is my code of my scales:

            scales: {
                x: {
                    type: "linear",
                    position: "left",
                    max: this.res.maxs[0],
                    min: 0,
                    gridLines: {
                        display: true,
                    },
                },

                y1: {
                    type: "linear",
                    position: "left",
                    stack: "demo",
                    stackWeight: 1,
                    max: this.res.maxs[4],
                    min: 0,
                    title: {
                        display: true,
                        text: this.axisLabels[3],
                        font: {
                            size: 11,
                        },
                    },
                },
                y2: {
                    type: "linear",
                    position: "left",
                    stack: "demo",
                    stackWeight: 1,
                    grid: {
                        borderColor: "blue",
                    },
                    max: this.res.maxs[2],
                    min: -10,
                    title: {
                        display: true,
                        text: this.axisLabels[1],
                        font: {
                            size: 11,
                        },
                    },
                    ticks: {
                        callback: (value, index, values) =>
                            index == 0 ? undefined : value.toFixed(1),
                    },
                },

               
            }

答案1

得分: 1

以下是翻译好的部分:

"chartjs-plugin-annotation" 插件是一个安全的解决方案,它具有 线性注释 功能。

可以通过以下选项在 y2 轴底部获得一条线:

options:{
// 其他选项...
plugins: {
// 其他插件...
annotation: {
annotations: {
line_y2_bottom: {
type: 'line',
scaleID: 'y2',
value: function({chart}, props){
const scale = chart.scales[props.scaleID];
return scale.getValueForPixel(scale.bottom - 1) || scale.min;
},
borderColor: 'black',
borderWidth: 3,
},
}
}
}
}

如果已知 value 的值,可以将其设置为固定数值。然而,value 以及大多数其他属性都是可编程的,这在轴具有 offset: true 的情况下是一个巨大的优势,因此实际的最小值(以及最大值)不仅由数据决定,还由于要使所有轴适应可用空间的目标而决定。

在上面的示例中,idline_y2_bottom 的线是任意的,我选择它对人类来说有意义,唯一的要求是每个注释都有不同的 id

以下是一个在每个图表底部绘制线的最小示例片段:

const x = Array.from({length: 101}, (_, i)=>({x: i/100}));
const data = {
datasets: [
{
data: x.map(({x})=>({x, y: Math.exp(x+1)})),
borderColor: 'red',
yAxisID: 'y1'
},
{
data: x.map(({x})=>({x, y: Math.exp(-x+1)})),
borderColor: 'blue',
yAxisID: 'y2',
},
{
data: x.map(({x})=>({x, y: Math.sin(x+1)})),
borderColor: 'green',
yAxisID: 'y3',
}
]
};
const config = {
type: 'line',
data: data,
options: {
responsive: true,
aspectRatio: 1,
plugins: {
legend: {
display: false,
},
annotation: {
annotations: Object.fromEntries(['y1', 'y2', 'y3'].map(
scaleID => [
`line_${scaleID}_bottom`,
{
type: 'line',
scaleID,
value: function({chart}, props){
const scale = chart.scales[props.scaleID];
return scale.getValueForPixel(scale.bottom-1) || scale.min;
},
borderColor: 'black',
borderWidth: 3,
}
])
)
}
},
scales: {
x: {
type: 'linear',
},
y1: {
stack: 'demo',
offset: true,
stackWeight: 1,
},
y2: {
stack: 'demo',
stackWeight: 2,
border:{
color: 'black',
width: 2
},
offset: true
},
y3: {
stack: 'demo',
offset: true,
stackWeight: 1
}
}
},
};
new Chart(document.querySelector('#chart1'), config);
<div style="width: 95vw">
<canvas id="chart1"></canvas>
</div>
<script src="https://cdnjs.cloudflare.com/ajax/libs/Chart.js/4.3.0/chart.umd.js" integrity="sha512-CMF3tQtjOoOJoOKlsS7/2loJlkyctwzSoDK/S40iAB+MqWSaf50uObGQSk5Ny/gfRhRCjNLvoxuCvdnERU4WGg==" crossorigin="anonymous" referrerpolicy="no-referrer"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/chartjs-plugin-annotation/3.0.1/chartjs-plugin-annotation.min.js" integrity="sha512-Hn1w6YiiFw6p6S2lXv6yKeqTk0PLVzeCwWY9n32beuPjQ5HLcvz5l2QsP+KilEr1ws37rCTw3bZpvfvVIeTh0Q==" crossorigin="anonymous" referrerpolicy="no-referrer"></script>

希望这对你有帮助。

英文:

A safe solution is to use the plugin chartjs-plugin-annotation that has a line annotation.

A line at the bottom of the axis y2 can be obtained by using the options:

options:{
// other options .....
plugins: {
// other plugins .... 
annotation: {
annotations: {
line_y2_bottom: {
type: &#39;line&#39;,
scaleID: &#39;y2&#39;,
value: function({chart}, props){
const scale = chart.scales[props.scaleID];
return scale.getValueForPixel(scale.bottom - 1) || scale.min;
},
borderColor: &#39;black&#39;,
borderWidth: 3,
},
}
}
}
}

If known apriori, the value can be set to a fixed numeric value. However, the fact that value, as well as most other properties, is scriptable is a great advantage in this context, when the axes have offset: true, so the actual minimal value (as well as maximal one) are not dictated exclusively by the data, but also by the objective of making all axes fit in the available space.

The line id, in the example above, line_y2_bottom is arbitrary, I chose it to be meaningful for humans, the only
requirement is that each annotation has a different id.

A minimal snippet example that draws a line at the bottom of each chart:

<!-- begin snippet: js hide: false console: false babel: false -->

<!-- language: lang-js -->

const x = Array.from({length: 101}, (_, i)=&gt;({x: i/100}));
const data = {
datasets: [
{
data: x.map(({x})=&gt;({x, y: Math.exp(x+1)})),
borderColor: &#39;red&#39;,
yAxisID: &#39;y1&#39;
},
{
data: x.map(({x})=&gt;({x, y: Math.exp(-x+1)})),
borderColor: &#39;blue&#39;,
yAxisID: &#39;y2&#39;,
},
{
data: x.map(({x})=&gt;({x, y: Math.sin(x+1)})),
borderColor: &#39;green&#39;,
yAxisID: &#39;y3&#39;,
}
]
};
const config = {
type: &#39;line&#39;,
data: data,
options: {
responsive: true,
aspectRatio: 1,
plugins: {
legend: {
display: false,
},
annotation: {
annotations: Object.fromEntries([&#39;y1&#39;, &#39;y2&#39;, &#39;y3&#39;].map(
scaleID =&gt; [
`line_${scaleID}_bottom`,
{
type: &#39;line&#39;,
scaleID,
value: function({chart}, props){
const scale = chart.scales[props.scaleID];
return scale.getValueForPixel(scale.bottom-1) || scale.min;
},
borderColor: &#39;black&#39;,
borderWidth: 3,
}
])
)
}
},
scales: {
x: {
type: &#39;linear&#39;,
},
y1: {
stack: &#39;demo&#39;,
offset: true,
stackWeight: 1,
},
y2: {
stack: &#39;demo&#39;,
stackWeight: 2,
border:{
color: &#39;black&#39;,
width: 2
},
offset: true
},
y3: {
stack: &#39;demo&#39;,
offset: true,
stackWeight: 1
}
}
},
};
new Chart(document.querySelector(&#39;#chart1&#39;), config);

<!-- language: lang-html -->

&lt;div style=&quot;width: 95vw&quot;&gt;
&lt;canvas id=&quot;chart1&quot;&gt;&lt;/canvas&gt;
&lt;/div&gt;
&lt;script src=&quot;https://cdnjs.cloudflare.com/ajax/libs/Chart.js/4.3.0/chart.umd.js&quot; integrity=&quot;sha512-CMF3tQtjOoOJoOKlsS7/2loJlkyctwzSoDK/S40iAB+MqWSaf50uObGQSk5Ny/gfRhRCjNLvoxuCvdnERU4WGg==&quot; crossorigin=&quot;anonymous&quot; referrerpolicy=&quot;no-referrer&quot;&gt;&lt;/script&gt;
&lt;script src=&quot;https://cdnjs.cloudflare.com/ajax/libs/chartjs-plugin-annotation/3.0.1/chartjs-plugin-annotation.min.js&quot; integrity=&quot;sha512-Hn1w6YiiFw6p6S2lXv6yKeqTk0PLVzeCwWY9n32beuPjQ5HLcvz5l2QsP+KilEr1ws37rCTw3bZpvfvVIeTh0Q==&quot; crossorigin=&quot;anonymous&quot; referrerpolicy=&quot;no-referrer&quot;&gt;&lt;/script&gt;

<!-- end snippet -->

huangapple
  • 本文由 发表于 2023年5月30日 02:53:17
  • 转载请务必保留本文链接:https://go.coder-hub.com/76359728.html
匿名

发表评论

匿名网友

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

确定