Created
June 6, 2019 14:36
-
-
Save mzsima/cd719c8044fc901cb54c255902e488f4 to your computer and use it in GitHub Desktop.
blazor_c3js_sparklines
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
window.CreateChart = (params) => { | |
var chart = c3.generate({ | |
bindto: '.' + params.uid, | |
data: { | |
x: 'x', | |
columns: [ | |
['x'].concat(params.x), | |
['sample'].concat(params.y) | |
] | |
}, | |
legend: {show: false}, | |
tooltip:{show:false}, | |
axis: { x: {show:false}, y: {show:false} }, | |
size: {height:30, width:50}, | |
point: {show: false} | |
}); | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<!DOCTYPE html> | |
<html> | |
<head> | |
<meta charset="utf-8" /> | |
<meta name="viewport" content="width=device-width" /> | |
<title>c3js_sparklines</title> | |
<base href="/" /> | |
<link href="css/bootstrap/bootstrap.min.css" rel="stylesheet" /> | |
<link href="css/site.css" rel="stylesheet" /> | |
<!-- Load c3.css --> | |
<link href="https://cdnjs.cloudflare.com/ajax/libs/c3/0.7.1/c3.min.css" rel="stylesheet"> | |
<!-- Load d3.js and c3.js --> | |
<script src="https://cdnjs.cloudflare.com/ajax/libs/d3/5.9.2/d3.min.js" charset="utf-8"></script> | |
<script src="https://cdnjs.cloudflare.com/ajax/libs/c3/0.7.1/c3.min.js"></script> | |
</head> | |
<body> | |
<app>Loading...</app> | |
<script src="helper.js"></script> | |
<script src="_framework/blazor.webassembly.js"></script> | |
</body> | |
</html> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
@page "/" | |
@using System.Collections.Generic | |
@inject IJSRuntime JsRuntime; | |
<h1>c3js sparklines</h1> | |
@if (records == null) | |
{ | |
<p><em>Loading...</em></p> | |
} | |
else | |
{ | |
<ul> | |
@foreach (var record in records) | |
{ | |
<li>@record.uid -- <span class=@record.uid></span></li> | |
} | |
</ul> | |
} | |
@functions{ | |
IList<UserRecord> records = new List<UserRecord>(); | |
protected override async Task OnInitAsync() | |
{ | |
IList<MyItem> items = new List<MyItem>(); | |
Random rnd = new Random(); | |
for (var i = 0; i < 200; i++) { | |
int x = rnd.Next(10, 200); | |
int y = rnd.Next(10, 200); | |
int rand_id = rnd.Next(10, 20); | |
items.Add(new MyItem {uid="U" + rand_id, x=x, y=y}); | |
} | |
// agg | |
records = items.Aggregate(new List<UserRecord>(), (acc, i) => { | |
var rec = acc.Where(a => a.uid == i.uid); | |
if (rec.Any()) { | |
rec.First().x.Add(i.x); | |
rec.First().y.Add(i.y); | |
} else { | |
acc.Add(new UserRecord { | |
uid = i.uid, | |
x = new List<int> { i.x }, | |
y = new List<int> { i.y }, | |
}); | |
} | |
return acc; | |
}); | |
} | |
protected override async Task OnAfterRenderAsync() { | |
foreach (var r in records) { | |
await JsRuntime.InvokeAsync<Object>("CreateChart",new { uid=r.uid, x=r.x, y=r.y }); | |
} | |
} | |
struct MyItem { | |
public string uid; | |
public int x; | |
public int y; | |
} | |
struct UserRecord { | |
public string uid; | |
public List<int> x; | |
public List<int> y; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment