Skip to content

Commit e99511c

Browse files
authored
Merge pull request #19 from macias/main
crosshair
2 parents 9d212d5 + 846f961 commit e99511c

File tree

7 files changed

+113
-3
lines changed

7 files changed

+113
-3
lines changed

ChartjsDemo/Pages/LineSimple.razor

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,19 @@ protected override async Task OnInitializedAsync()
5050
{
5151
_config1 = new LineChartConfig()
5252
{
53+
Options = new Options()
54+
{
55+
Plugins = new Plugins()
56+
{
57+
Crosshair = new Crosshair()
58+
{
59+
Vertical = new CrosshairLine()
60+
{
61+
Color = Colors.PaletteBorder1[0],
62+
}
63+
}
64+
}
65+
}
5366
};
5467

5568
_config1.Data.Labels = LineDataExamples.SimpleLineText;

src/Chart.razor

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
@inject IJSRuntime JSRuntime
22

33
<div id="@("chartcontainer" + Config.CanvasId)" class="chart-container @Class"
4-
style="height:@Height; width:@Width; @(Style)" @onmouseout="async (args) => await OnMouseOut(args)">
4+
style="height:@Height; width:@Width; @(Style)" @onmouseout="async (args) => await OnMouseOutAsync(args)">
55
<canvas id="@Config.CanvasId" style="@(Height != null ? $"height:{Height}" : "") @(Width != null ? $"width:{Width};" : "")"></canvas>
66
</div>

src/Chart.razor.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -105,7 +105,7 @@ public void Dispose()
105105
this.oldReference?.Dispose();
106106
}
107107

108-
private ValueTask OnMouseOut(MouseEventArgs mouseEventArgs)
108+
private ValueTask OnMouseOutAsync(MouseEventArgs mouseEventArgs)
109109
{
110110
if (Config.Options is Options { OnMouseOutAsync: { } } options)
111111
return options.OnMouseOutAsync(mouseEventArgs);

src/Models/Common/Crosshair.cs

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
namespace PSC.Blazor.Components.Chartjs.Models.Common
2+
{
3+
public sealed class Crosshair
4+
{
5+
[JsonPropertyName("cursor")]
6+
[JsonIgnore(Condition = JsonIgnoreCondition.WhenWritingNull)]
7+
public string? Cursor { get; set; }
8+
9+
[JsonPropertyName("vertical")]
10+
[JsonIgnore(Condition = JsonIgnoreCondition.WhenWritingNull)]
11+
public CrosshairLine? Vertical { get; set; }
12+
13+
[JsonPropertyName("horizontal")]
14+
[JsonIgnore(Condition = JsonIgnoreCondition.WhenWritingNull)]
15+
public CrosshairLine? Horizontal { get; set; }
16+
17+
}
18+
}

src/Models/Common/CrosshairLine.cs

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
namespace PSC.Blazor.Components.Chartjs.Models.Common
2+
{
3+
public sealed class CrosshairLine
4+
{
5+
[JsonPropertyName("color")]
6+
[JsonIgnore(Condition = JsonIgnoreCondition.WhenWritingNull)]
7+
public string? Color { get; set; }
8+
9+
[JsonPropertyName("width")]
10+
public int Width { get; set; } = 1;
11+
12+
[JsonPropertyName("dash")]
13+
[JsonIgnore(Condition = JsonIgnoreCondition.WhenWritingNull)]
14+
public int[]? Dash { get; set; }
15+
16+
}
17+
}

src/Models/Common/Plugins.cs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,5 +24,9 @@ public class Plugins
2424
[JsonPropertyName("tooltip")]
2525
[JsonIgnore(Condition = JsonIgnoreCondition.WhenWritingNull)]
2626
public Tooltip? Tooltip { get; set; }
27+
28+
[JsonPropertyName("crosshair")]
29+
[JsonIgnore(Condition = JsonIgnoreCondition.WhenWritingNull)]
30+
public Crosshair? Crosshair { get; set; }
2731
}
2832
}

src/wwwroot/Chart.js

Lines changed: 59 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,51 @@
1-
window.setup = (id, dotnetConfig, jsonConfig) => {
1+
function crosshairLine(chart,evt,plugin)
2+
{
3+
// https://www.youtube.com/watch?v=M3SOJJOf6L8
4+
const { canvas, ctx, chartArea: {left, right, top, bottom}}=chart;
5+
6+
chart.update("none");
7+
8+
if (plugin.cursor) {
9+
if (evt.offsetX >= left && evt.offsetX <= right && evt.offsetY <= bottom && evt.offsetY >= top) {
10+
canvas.style.cursor = plugin.cursor;
11+
} else
12+
canvas.style.cursor = "default";
13+
}
14+
15+
if (plugin.vertical && evt.offsetX>=left && evt.offsetX<=right) {
16+
let line = plugin.vertical;
17+
18+
ctx.save();
19+
ctx.beginPath();
20+
ctx.moveTo(evt.offsetX, top);
21+
ctx.lineTo(evt.offsetX, bottom);
22+
ctx.lineWidth = line.width;
23+
if (line.color)
24+
ctx.strokeStyle = line.color;
25+
if (line.dash)
26+
ctx.setLineDash(line.dash);
27+
ctx.stroke();
28+
ctx.restore();
29+
}
30+
31+
if (plugin.horizontal && evt.offsetY<=bottom && evt.offsetY>=top) {
32+
let line = plugin.horizontal;
33+
34+
ctx.save();
35+
ctx.beginPath();
36+
ctx.moveTo(left, evt.offsetY);
37+
ctx.lineTo(right, evt.offsetY);
38+
ctx.lineWidth = line.width;
39+
if (line.color)
40+
ctx.strokeStyle = line.color;
41+
if (line.dash)
42+
ctx.setLineDash(line.dash);
43+
ctx.stroke();
44+
ctx.restore();
45+
}
46+
}
47+
48+
window.setup = (id, dotnetConfig, jsonConfig) => {
249
document.getElementById("chartcontainer" + id).style.display = 'none';
350
document.getElementById("chartcontainer" + id).innerHTML = '&nbsp;';
451
document.getElementById("chartcontainer" + id).innerHTML = '<canvas id="' + id + '"></canvas>';
@@ -14,6 +61,12 @@
1461
};
1562
}
1663

64+
let crosshair_plugin = config?.options?.plugins?.crosshair;
65+
if (config?.options?.plugins?.crosshair)
66+
{
67+
config.options.plugins.crosshair = undefined;
68+
}
69+
1770
if (config?.options?.hasOnHoverAsync) {
1871
config.options.hasOnHoverAsync = undefined;
1972
config.options.onHover = function (evt, activeElements, ch) {
@@ -29,6 +82,11 @@
2982
}
3083

3184
var chart = new Chart(context2d, config);
85+
if (crosshair_plugin) {
86+
chart.canvas.addEventListener("mousemove", (evt) => {
87+
crosshairLine(chart, evt,crosshair_plugin);
88+
});
89+
}
3290

3391
chart.options.onClick = function (event, array) {
3492
var rtn = -1;

0 commit comments

Comments
 (0)