Skip to content

Commit

Permalink
Updated for Blazor 0.3 release with OnAfterRender goodness.
Browse files Browse the repository at this point in the history
  • Loading branch information
muqeet-khan committed May 3, 2018
1 parent eeef3da commit 12642ef
Show file tree
Hide file tree
Showing 35 changed files with 1,325 additions and 561 deletions.
116 changes: 60 additions & 56 deletions README.md
Expand Up @@ -39,67 +39,74 @@ dotnet add package BlazorComponents
1. In cshtml file add this:

```html
<ChartJsLineChart Chart="@blazorLineChartJS" Width="600" Height="300" UpdateChart="@((chart) => { UpdateChart(chart); })" />
<div class="row">
<button class="btn btn-primary" onclick="@UpdateChart">Update Chart </button>
</div>
<ChartJsLineChart ref="lineChartJs" Chart="@blazorLineChartJS" Width="600" Height="300" />
```

```csharp
@functions {
@functions{

public ChartJSChart<ChartJsLineDataset> blazorLineChartJS { get; set; } = new ChartJSChart<ChartJsLineDataset>();
public ChartJSChart<ChartJsLineDataset> blazorLineChartJS { get; set; } = new ChartJSChart<ChartJsLineDataset>();
ChartJsLineChart lineChartJs;

protected override void OnInit()
{
blazorLineChartJS = new ChartJSChart<ChartJsLineDataset>()
protected override void OnInit()
{
ChartType = "line",
CanvasId = "myFirstLineChart",
Options = new ChartJsOptions()
{
Text = "Sample chart from Blazor",
Display = true
},
Data = new ChartJsData<ChartJsLineDataset>()

blazorLineChartJS = new ChartJSChart<ChartJsLineDataset>()
{
Labels = new List<string>() { "Red", "Blue", "Yellow", "Green", "Purple", "Orange" },
Datasets = new List<ChartJsLineDataset>()
ChartType = "line",
CanvasId = "myFirstLineChart",
Options = new ChartJsOptions()
{
Text = "Sample chart from Blazor",
Display = true,
Responsive = false
},
Data = new ChartJsData<ChartJsLineDataset>()
{
new ChartJsLineDataset()
{
BackgroundColor = "#ff6384",
BorderColor = "#ff6384",
Label = "# of Votes from blazor",
Data = new List<int>{ 19,12,5,3,3,2},
Fill = true,
BorderWidth = 2,
PointRadius = 3,
PointBorderWidth=1
}
Labels = new List<string>() { "Red", "Blue", "Yellow", "Green", "Purple", "Orange" },
Datasets = new List<ChartJsLineDataset>()
{
new ChartJsLineDataset()
{
BackgroundColor = "#ff6384",
BorderColor = "#ff6384",
Label = "# of Votes from blazor",
Data = new List<int>{ 19,12,5,3,3,2},
Fill = false,
BorderWidth = 2,
PointRadius = 3,
PointBorderWidth=1
}
}
}
}
};
}

};
}

public void UpdateChart(ChartJSChart<ChartJsLineDataset> chartItem)
{
//Update existing dataset
chartItem.Data.Labels.Add($"New{DateTime.Now.Second}");
var firstDataSet = chartItem.Data.Datasets[0];
firstDataSet.Data.Add(DateTime.Now.Second);

//Add new dataset
//chartItem.Data.Datasets.Add(new ChartJsLineDataset()
//{
// BackgroundColor = "#cc65fe",
// BorderColor = "#cc65fe",
// Label = "# of Votes from blazor 1",
// Data = new List<int> {20,21,12,3,4,4},
// Fill = true,
// BorderWidth = 2,
// PointRadius = 3,
// PointBorderWidth = 1
//});
}
public void UpdateChart()
{
//Update existing dataset
blazorLineChartJS.Data.Labels.Add($"New{DateTime.Now.Second}");
var firstDataSet = blazorLineChartJS.Data.Datasets[0];
firstDataSet.Data.Add(DateTime.Now.Second);

//Add new dataset
//blazorLineChartJS.Data.Datasets.Add(new ChartJsLineDataset()
//{
// BackgroundColor = "#cc65fe",
// BorderColor = "#cc65fe",
// Label = "# of Votes from blazor 1",
// Data = new List<int> {20,21,12,3,4,4},
// Fill = true,
// BorderWidth = 2,
// PointRadius = 3,
// PointBorderWidth = 1
//});
lineChartJs.UpdateChart(blazorLineChartJS);
}
}
```

Expand All @@ -109,17 +116,14 @@ public void UpdateChart(ChartJSChart<ChartJsLineDataset> chartItem)
<script src="//cdnjs.cloudflare.com/ajax/libs/Chart.js/2.7.2/Chart.min.js"></script>
<script type="blazor-boot">
</script>
<script src="BlazorComponents.js"></script>
```


## Sample Output

Run the project and you will need to click on the Display Chart button. To know more about this limitation read [#495](https://github.com/aspnet/Blazor/issues/495) on the Blazor repo.

Line Chart Example:
![LineChart](linechart.png)

Bar Chart Example:
![BarChart](barchart.png)
![Barchart](barchart.png)


Binary file modified barchart.png
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file modified linechart.png
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
36 changes: 32 additions & 4 deletions samples/TestApplication/Pages/Index.cshtml
@@ -1,14 +1,19 @@
@page "/"
@using BlazorComponents.Shared


<h2>Chart JS charts using Blazor</h2>

<ChartJsBarChart Chart="@blazorBarChartJS" Width="600" Height="300" />
<div class="container">
<h2>Chart JS charts using Blazor</h2>
<div class="row">
<button class="btn btn-primary" onclick="@UpdateChart">Update Chart </button>
</div>
<ChartJsBarChart ref="barChartJs" Chart="@blazorBarChartJS" Width="600" Height="300" />
</div>


@functions {

public ChartJSChart<ChartJsBarDataset> blazorBarChartJS { get; set; } = new ChartJSChart<ChartJsBarDataset>();
ChartJsBarChart barChartJs;

protected override void OnInit()
{
Expand Down Expand Up @@ -45,4 +50,27 @@
}
};
}

public void UpdateChart()
{
//Update existing dataset
blazorBarChartJS.Data.Labels.Add($"New{DateTime.Now.Second}");
var firstDataSet = blazorBarChartJS.Data.Datasets[0];
firstDataSet.Data.Add(DateTime.Now.Second);

//Add new dataset
//blazorLineChartJS.Data.Datasets.Add(new ChartJsLineDataset()
//{
// BackgroundColor = "#cc65fe",
// BorderColor = "#cc65fe",
// Label = "# of Votes from blazor 1",
// Data = new List<int> {20,21,12,3,4,4},
// Fill = true,
// BorderWidth = 2,
// PointRadius = 3,
// PointBorderWidth = 1
//});
barChartJs.UpdateChart(blazorBarChartJS);
}
}
108 changes: 57 additions & 51 deletions samples/TestApplication/Pages/LineChart.cshtml
@@ -1,65 +1,71 @@
@page "/LineChart"

@using BlazorComponents.Shared

<h2>Chart JS charts using Blazor</h2>
<div class="row">
<button class="btn btn-primary" onclick="@UpdateChart">Update Chart </button>
</div>
<ChartJsLineChart ref="lineChartJs" Chart="@blazorLineChartJS" Width="600" Height="300" />

<ChartJsLineChart Chart="@blazorLineChartJS" Width="600" Height="300" UpdateChart="@((chart) => { UpdateChart(chart); })" />

@functions {
@functions{

public ChartJSChart<ChartJsLineDataset> blazorLineChartJS { get; set; } = new ChartJSChart<ChartJsLineDataset>();
public ChartJSChart<ChartJsLineDataset> blazorLineChartJS { get; set; } = new ChartJSChart<ChartJsLineDataset>();
ChartJsLineChart lineChartJs;

protected override void OnInit()
{
blazorLineChartJS = new ChartJSChart<ChartJsLineDataset>()
protected override void OnInit()
{
ChartType = "line",
CanvasId = "myFirstLineChart",
Options = new ChartJsOptions()
{
Text = "Sample chart from Blazor",
Display = true
},
Data = new ChartJsData<ChartJsLineDataset>()

blazorLineChartJS = new ChartJSChart<ChartJsLineDataset>()
{
Labels = new List<string>() { "Red", "Blue", "Yellow", "Green", "Purple", "Orange" },
Datasets = new List<ChartJsLineDataset>()
ChartType = "line",
CanvasId = "myFirstLineChart",
Options = new ChartJsOptions()
{
Text = "Sample chart from Blazor",
Display = true,
Responsive = false
},
Data = new ChartJsData<ChartJsLineDataset>()
{
new ChartJsLineDataset()
{
BackgroundColor = "#ff6384",
BorderColor = "#ff6384",
Label = "# of Votes from blazor",
Data = new List<int>{ 19,12,5,3,3,2},
Fill = true,
BorderWidth = 2,
PointRadius = 3,
PointBorderWidth=1
}
Labels = new List<string>() { "Red", "Blue", "Yellow", "Green", "Purple", "Orange" },
Datasets = new List<ChartJsLineDataset>()
{
new ChartJsLineDataset()
{
BackgroundColor = "#ff6384",
BorderColor = "#ff6384",
Label = "# of Votes from blazor",
Data = new List<int>{ 19,12,5,3,3,2},
Fill = false,
BorderWidth = 2,
PointRadius = 3,
PointBorderWidth=1
}
}
}
}
};
}
};
}

public void UpdateChart()
{
//Update existing dataset
blazorLineChartJS.Data.Labels.Add($"New{DateTime.Now.Second}");
var firstDataSet = blazorLineChartJS.Data.Datasets[0];
firstDataSet.Data.Add(DateTime.Now.Second);

public void UpdateChart(ChartJSChart<ChartJsLineDataset> chartItem)
{
//Update existing dataset
chartItem.Data.Labels.Add($"New{DateTime.Now.Second}");
var firstDataSet = chartItem.Data.Datasets[0];
firstDataSet.Data.Add(DateTime.Now.Second);
//Add new dataset
//blazorLineChartJS.Data.Datasets.Add(new ChartJsLineDataset()
//{
// BackgroundColor = "#cc65fe",
// BorderColor = "#cc65fe",
// Label = "# of Votes from blazor 1",
// Data = new List<int> {20,21,12,3,4,4},
// Fill = true,
// BorderWidth = 2,
// PointRadius = 3,
// PointBorderWidth = 1
//});
//Add new dataset
//chartItem.Data.Datasets.Add(new ChartJsLineDataset()
//{
// BackgroundColor = "#cc65fe",
// BorderColor = "#cc65fe",
// Label = "# of Votes from blazor 1",
// Data = new List<int> {20,21,12,3,4,4},
// Fill = true,
// BorderWidth = 2,
// PointRadius = 3,
// PointBorderWidth = 1
//});
}
lineChartJs.UpdateChart(blazorLineChartJS);
}
}
72 changes: 72 additions & 0 deletions samples/TestApplication/Pages/RadarChart.cshtml
@@ -0,0 +1,72 @@
@page "/RadarChart"
@using BlazorComponents.Shared


<h2>Chart JS charts using Blazor</h2>
<div class="row">
<button class="btn btn-primary" onclick="@UpdateChart">Update Chart </button>
</div>
<ChartJsRadarChart ref="radarChartJs" Chart="@blazorRadarChartJS" Width="600" Height="300"/>

@functions {

public ChartJSChart<ChartJSRadarDataset> blazorRadarChartJS { get; set; } = new ChartJSChart<ChartJSRadarDataset>();
ChartJsRadarChart radarChartJs;

protected override void OnInit()
{

blazorRadarChartJS = new ChartJSChart<ChartJSRadarDataset>()
{
ChartType = "line",
CanvasId = "myFirstRadarChart",
Options = new ChartJsOptions()
{
Text = "Sample chart from Blazor",
Display = true
},
Data = new ChartJsData<ChartJSRadarDataset>()
{
Labels = new List<string>() { "Eating", "Drinking", "Social", "Coding", "Cycling", "Running" },
Datasets = new List<ChartJSRadarDataset>()
{
new ChartJSRadarDataset()
{
BackgroundColor = "#ff6384",
Label = "1st Dataset",
Data = new List<int>{ 19,12,5,3,3,2},
BorderWidth = 2,
RadarChartPointStyle = RadarChartPointStyles.star,
PointBackgroundColor = "#a4cef0",
PointRadius = 5,
PointBorderWidth=1
}
}
}
};
}


public void UpdateChart()
{
//Update existing dataset
blazorRadarChartJS.Data.Labels.Add($"New{DateTime.Now.Second}");
var firstDataSet = blazorRadarChartJS.Data.Datasets[0];
firstDataSet.Data.Add(DateTime.Now.Second);

//Add new dataset
blazorRadarChartJS.Data.Datasets.Add(new ChartJSRadarDataset()
{
BackgroundColor = "#cc65fe",
BorderColor = "#cc65fe",
Label = "2nd Dataset",
Data = new List<int> { 20, 21, 12, 3, 4, 4 },
BorderWidth = 2,
PointRadius = 3,
PointBorderWidth = 1
});

radarChartJs.UpdateChart(blazorRadarChartJS);
}

}

0 comments on commit 12642ef

Please sign in to comment.