WPF C#/VB
Charts: Part 2 (Customising a Chart)
C#
// AZUL CODING ---------------------------------------
// WPF C#/VB - Charts: Part 2 (Customising a Chart)
// https://youtu.be/UL6fj5GLsto
using System;
using System.Windows;
using System.Windows.Controls;
using DataVis = System.Windows.Forms.DataVisualization;
using System.Drawing;
namespace AzulChart
{
/// <summary>
/// Interaction logic for MainWindow.xaml
/// </summary>
public partial class MainWindow : Window
{
public MainWindow()
{
InitializeComponent();
Chart1.Series[0].Points.Add(3.0).AxisLabel = "Sample data";
Chart1.Series[0].BorderWidth = 4;
Chart1.Series[0].LabelBackColor = System.Drawing.Color.White;
Chart1.Series[0].Font = new Font("Segoe UI", 12);
Chart1.ChartAreas[0].AxisX.TitleFont = new Font("Segoe UI", 12);
Chart1.ChartAreas[0].AxisY.TitleFont = new Font("Segoe UI", 12);
Chart1.ChartAreas[0].AxisX.LabelStyle.Font = new Font("Segoe UI", 12);
Chart1.ChartAreas[0].AxisY.LabelStyle.Font = new Font("Segoe UI", 12);
}
private void ChartType_Click(object sender, RoutedEventArgs e)
{
switch (((RadioButton)sender).Name)
{
case "ColRadio":
Chart1.Series[0].ChartType = DataVis.Charting.SeriesChartType.Column;
break;
case "BarRadio":
Chart1.Series[0].ChartType = DataVis.Charting.SeriesChartType.Bar;
break;
case "PieRadio":
Chart1.Series[0].ChartType = DataVis.Charting.SeriesChartType.Pie;
break;
case "LineRadio":
Chart1.Series[0].ChartType = DataVis.Charting.SeriesChartType.Line;
break;
default:
break;
}
}
private void UpdateBtn_Click(object sender, RoutedEventArgs e)
{
// Sample data,3
// Example label,6
Chart1.Series[0].Points.Clear();
foreach (var point in DataTextBox.Text.Split(new[] { "\r\n", "\r", "\n" }, StringSplitOptions.None))
{
// Sample data,3
var info = point.Split(new[] { "," }, StringSplitOptions.None);
var label = info[0];
var value = double.Parse(info[1]);
Chart1.Series[0].Points.Add(value).AxisLabel = label;
}
}
private void ChartTitle_Changed(object sender, TextChangedEventArgs e)
{
if (ChartTitleTextBox.Text == "")
{
Chart1.Titles.Clear();
}
else
{
if (Chart1.Titles.Count == 0)
{
Chart1.Titles.Add(ChartTitleTextBox.Text);
Chart1.Titles[0].Font = new Font("Segoe UI", 15, System.Drawing.FontStyle.Bold);
}
else
{
Chart1.Titles[0].Text = ChartTitleTextBox.Text;
}
}
}
private void XAxis_Changed(object sender, TextChangedEventArgs e)
{
Chart1.ChartAreas[0].AxisX.Title = XAxisTitleTextBox.Text;
}
private void YAxis_Changed(object sender, TextChangedEventArgs e)
{
Chart1.ChartAreas[0].AxisY.Title = YAxisTitleTextBox.Text;
}
private void ColourScheme_Changed(object sender, SelectionChangedEventArgs e)
{
switch (ColourDropDown.SelectedIndex)
{
case 0:
Chart1.Palette = DataVis.Charting.ChartColorPalette.BrightPastel;
break;
case 1:
Chart1.Palette = DataVis.Charting.ChartColorPalette.Berry;
break;
case 2:
Chart1.Palette = DataVis.Charting.ChartColorPalette.Pastel;
break;
case 3:
Chart1.Palette = DataVis.Charting.ChartColorPalette.Grayscale;
break;
case 4:
Chart1.Palette = DataVis.Charting.ChartColorPalette.SemiTransparent;
break;
default:
break;
}
}
private void Labels_Click(object sender, RoutedEventArgs e)
{
Chart1.Series[0].IsValueShownAsLabel = (bool)LabelsCheckBox.IsChecked;
}
private void Export_Click(object sender, RoutedEventArgs e)
{
var ChartBmp = new Bitmap(Chart1.Size.Width, Chart1.Size.Height);
var ChartBounds = new System.Drawing.Rectangle(0, 0, Chart1.Size.Width, Chart1.Size.Height);
Chart1.DrawToBitmap(ChartBmp, ChartBounds);
ChartBmp.Save(Environment.GetFolderPath(Environment.SpecialFolder.MyPictures) + "\\chart.png");
MessageBox.Show("Chart exported.");
}
}
}
VB.NET
' AZUL CODING ---------------------------------------
' WPF C#/VB - Charts: Part 2 (Customising a Chart)
' https://youtu.be/UL6fj5GLsto
Imports DataVis = System.Windows.Forms.DataVisualization
Imports System.Drawing
Class MainWindow
Public Sub New()
InitializeComponent()
Chart1.Series(0).Points.Add(3.0).AxisLabel = "Sample data"
Chart1.Series(0).BorderWidth = 4
Chart1.Series(0).LabelBackColor = System.Drawing.Color.White
Chart1.Series(0).Font = New Font("Segoe UI", 12)
Chart1.ChartAreas(0).AxisX.TitleFont = New Font("Segoe UI", 12)
Chart1.ChartAreas(0).AxisY.TitleFont = New Font("Segoe UI", 12)
Chart1.ChartAreas(0).AxisX.LabelStyle.Font = New Font("Segoe UI", 12)
Chart1.ChartAreas(0).AxisY.LabelStyle.Font = New Font("Segoe UI", 12)
End Sub
Private Sub ChartType_Click(ByVal sender As Object, ByVal e As RoutedEventArgs)
Select Case (CType(sender, RadioButton)).Name
Case "ColRadio"
Chart1.Series(0).ChartType = DataVis.Charting.SeriesChartType.Column
Case "BarRadio"
Chart1.Series(0).ChartType = DataVis.Charting.SeriesChartType.Bar
Case "PieRadio"
Chart1.Series(0).ChartType = DataVis.Charting.SeriesChartType.Pie
Case "LineRadio"
Chart1.Series(0).ChartType = DataVis.Charting.SeriesChartType.Line
Case Else
End Select
End Sub
Private Sub UpdateBtn_Click(ByVal sender As Object, ByVal e As RoutedEventArgs)
Chart1.Series(0).Points.Clear()
For Each point In DataTextBox.Text.Split({vbCrLf, vbCr, vbLf}, StringSplitOptions.None)
Dim info = point.Split({","}, StringSplitOptions.None)
Dim label = info(0)
Dim value = Double.Parse(info(1))
Chart1.Series(0).Points.Add(value).AxisLabel = label
Next
End Sub
Private Sub ChartTitle_Changed(ByVal sender As Object, ByVal e As TextChangedEventArgs)
If ChartTitleTextBox.Text = "" Then
Chart1.Titles.Clear()
Else
If Chart1.Titles.Count = 0 Then
Chart1.Titles.Add(ChartTitleTextBox.Text)
Chart1.Titles(0).Font = New Font("Segoe UI", 15, System.Drawing.FontStyle.Bold)
Else
Chart1.Titles(0).Text = ChartTitleTextBox.Text
End If
End If
End Sub
Private Sub XAxis_Changed(ByVal sender As Object, ByVal e As TextChangedEventArgs)
Chart1.ChartAreas(0).AxisX.Title = XAxisTitleTextBox.Text
End Sub
Private Sub YAxis_Changed(ByVal sender As Object, ByVal e As TextChangedEventArgs)
Chart1.ChartAreas(0).AxisY.Title = YAxisTitleTextBox.Text
End Sub
Private Sub ColourScheme_Changed(ByVal sender As Object, ByVal e As SelectionChangedEventArgs)
Select Case ColourDropDown.SelectedIndex
Case 0
Chart1.Palette = DataVis.Charting.ChartColorPalette.BrightPastel
Case 1
Chart1.Palette = DataVis.Charting.ChartColorPalette.Berry
Case 2
Chart1.Palette = DataVis.Charting.ChartColorPalette.Pastel
Case 3
Chart1.Palette = DataVis.Charting.ChartColorPalette.Grayscale
Case 4
Chart1.Palette = DataVis.Charting.ChartColorPalette.SemiTransparent
Case Else
End Select
End Sub
Private Sub Labels_Click(ByVal sender As Object, ByVal e As RoutedEventArgs)
Chart1.Series(0).IsValueShownAsLabel = CBool(LabelsCheckBox.IsChecked)
End Sub
Private Sub Export_Click(ByVal sender As Object, ByVal e As RoutedEventArgs)
Dim ChartBmp = New Bitmap(Chart1.Size.Width, Chart1.Size.Height)
Dim ChartBounds = New System.Drawing.Rectangle(0, 0, Chart1.Size.Width, Chart1.Size.Height)
Chart1.DrawToBitmap(ChartBmp, ChartBounds)
ChartBmp.Save(Environment.GetFolderPath(Environment.SpecialFolder.MyPictures) & "\chart.png")
MessageBox.Show("Chart exported.")
End Sub
End Class
XAML
<!-- AZUL CODING --------------------------------------- -->
<!-- WPF C#/VB - Charts: Part 2 (Customising a Chart) -->
<!-- https://youtu.be/UL6fj5GLsto -->
<Window x:Class="AzulChart.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:wf="clr-namespace:System.Windows.Forms.DataVisualization.Charting;assembly=System.Windows.Forms.DataVisualization"
mc:Ignorable="d"
Title="Charts - Azul Coding" Height="600" Width="850" ResizeMode="CanMinimize">
<Grid>
<Grid.ColumnDefinitions>
<ColumnDefinition MinWidth="500"/>
<ColumnDefinition/>
</Grid.ColumnDefinitions>
<Grid Margin="20,20,0,20" Grid.Column="0">
<WindowsFormsHost>
<wf:Chart x:Name="Chart1">
<wf:Chart.Series>
<wf:Series ChartType="Column"/>
</wf:Chart.Series>
<wf:Chart.ChartAreas>
<wf:ChartArea/>
</wf:Chart.ChartAreas>
</wf:Chart>
</WindowsFormsHost>
</Grid>
<StackPanel Grid.Column="1" Margin="20">
<Label Content="Chart type" FontSize="14"/>
<StackPanel Orientation="Horizontal">
<RadioButton Name="ColRadio" Content="Column" GroupName="ChartTypeRadios" Margin="10,5,0,0" IsChecked="True" Click="ChartType_Click"/>
<RadioButton Name="BarRadio" Content="Bar" GroupName="ChartTypeRadios" Margin="20,5,0,0" Click="ChartType_Click"/>
<RadioButton Name="PieRadio" Content="Pie" GroupName="ChartTypeRadios" Margin="20,5,0,0" Click="ChartType_Click"/>
<RadioButton Name="LineRadio" Content="Line" GroupName="ChartTypeRadios" Margin="20,5,0,0" Click="ChartType_Click"/>
</StackPanel>
<Label Content="Chart data" FontSize="14" Margin="0,20,0,0"/>
<TextBox Name="DataTextBox" Text="Sample data,3" AcceptsReturn="True" Margin="10,5,10,0" Height="70" Padding="5"/>
<StackPanel Orientation="Horizontal" Margin="0,5,0,0">
<Button Name="UpdateBtn" Content="Update" HorizontalAlignment="Left" Padding="20,5" Margin="10,0,0,0" Click="UpdateBtn_Click"/>
<CheckBox Name="LabelsCheckBox" Content="Show value labels" Margin="15,0,0,0" VerticalAlignment="Center" Click="Labels_Click"/>
</StackPanel>
<Label Content="Titles" FontSize="14" Margin="0,20,0,0"/>
<DockPanel Margin="0,5,0,0">
<Label Content="Chart title:" VerticalAlignment="Center" Width="70" Margin="5,0,0,0"/>
<TextBox Name="ChartTitleTextBox" Text="Chart" Margin="10,0" Padding="5" TextChanged="ChartTitle_Changed"/>
</DockPanel>
<DockPanel Margin="0,5,0,0">
<Label Content="X axis title:" VerticalAlignment="Center" Width="70" Margin="5,0,0,0"/>
<TextBox Name="XAxisTitleTextBox" Text="X axis" Margin="10,0" Padding="5" TextChanged="XAxis_Changed"/>
</DockPanel>
<DockPanel Margin="0,5,0,0">
<Label Content="Y axis title:" VerticalAlignment="Center" Width="70" Margin="5,0,0,0"/>
<TextBox Name="YAxisTitleTextBox" Text="Y axis" Margin="10,0" Padding="5" TextChanged="YAxis_Changed"/>
</DockPanel>
<Label Content="Colour scheme" FontSize="14" Margin="0,20,0,0"/>
<ComboBox Name="ColourDropDown" Margin="10,5,10,0" Padding="5" SelectionChanged="ColourScheme_Changed">
<ComboBoxItem Content="Default" IsSelected="True"/>
<ComboBoxItem Content="Berry"/>
<ComboBoxItem Content="Pastel"/>
<ComboBoxItem Content="Greyscale"/>
<ComboBoxItem Content="Semi-transparent"/>
</ComboBox>
<Label Content="Export as image" FontSize="14" Margin="0,20,0,0"/>
<Button Name="ExportBtn" Content="Export" HorizontalAlignment="Left" Padding="20,5" Margin="10,5,0,0" Click="Export_Click"/>
</StackPanel>
</Grid>
</Window>