Skip to content

Commit 7fa55b4

Browse files
Merge pull request #1 from SyncfusionExamples/Attach_the_sample
Attach the Dynamic Target Line WPF Chart KB sample
2 parents e17a048 + d00a2fe commit 7fa55b4

File tree

10 files changed

+488
-2
lines changed

10 files changed

+488
-2
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
2+
Microsoft Visual Studio Solution File, Format Version 12.00
3+
# Visual Studio Version 17
4+
VisualStudioVersion = 17.12.35506.116 d17.12
5+
MinimumVisualStudioVersion = 10.0.40219.1
6+
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "DynamicTargetLineSample", "DynamicTargetLineSample\DynamicTargetLineSample.csproj", "{59A4FB8C-77CC-40EF-87C4-4ECA9BBE9F8E}"
7+
EndProject
8+
Global
9+
GlobalSection(SolutionConfigurationPlatforms) = preSolution
10+
Debug|Any CPU = Debug|Any CPU
11+
Release|Any CPU = Release|Any CPU
12+
EndGlobalSection
13+
GlobalSection(ProjectConfigurationPlatforms) = postSolution
14+
{59A4FB8C-77CC-40EF-87C4-4ECA9BBE9F8E}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
15+
{59A4FB8C-77CC-40EF-87C4-4ECA9BBE9F8E}.Debug|Any CPU.Build.0 = Debug|Any CPU
16+
{59A4FB8C-77CC-40EF-87C4-4ECA9BBE9F8E}.Release|Any CPU.ActiveCfg = Release|Any CPU
17+
{59A4FB8C-77CC-40EF-87C4-4ECA9BBE9F8E}.Release|Any CPU.Build.0 = Release|Any CPU
18+
EndGlobalSection
19+
GlobalSection(SolutionProperties) = preSolution
20+
HideSolutionNode = FALSE
21+
EndGlobalSection
22+
EndGlobal
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
<Application x:Class="DynamicTargetLineSample.App"
2+
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
3+
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
4+
xmlns:local="clr-namespace:DynamicTargetLineSample"
5+
StartupUri="MainWindow.xaml">
6+
<Application.Resources>
7+
8+
</Application.Resources>
9+
</Application>
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
using System.Configuration;
2+
using System.Data;
3+
using System.Windows;
4+
5+
namespace DynamicTargetLineSample
6+
{
7+
/// <summary>
8+
/// Interaction logic for App.xaml
9+
/// </summary>
10+
public partial class App : Application
11+
{
12+
}
13+
14+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
using System.Windows;
2+
3+
[assembly: ThemeInfo(
4+
ResourceDictionaryLocation.None, //where theme specific resource dictionaries are located
5+
//(used if a resource is not found in the page,
6+
// or application resource dictionaries)
7+
ResourceDictionaryLocation.SourceAssembly //where the generic resource dictionary is located
8+
//(used if a resource is not found in the page,
9+
// app, or any theme specific resource dictionaries)
10+
)]
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
<Project Sdk="Microsoft.NET.Sdk">
2+
3+
<PropertyGroup>
4+
<OutputType>WinExe</OutputType>
5+
<TargetFramework>net9.0-windows</TargetFramework>
6+
<Nullable>enable</Nullable>
7+
<ImplicitUsings>enable</ImplicitUsings>
8+
<UseWPF>true</UseWPF>
9+
</PropertyGroup>
10+
11+
<ItemGroup>
12+
<PackageReference Include="Syncfusion.SfChart.WPF" Version="*" />
13+
</ItemGroup>
14+
15+
</Project>
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,74 @@
1+
<Window x:Class="DynamicTargetLineSample.MainWindow"
2+
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
3+
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
4+
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
5+
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
6+
xmlns:local="clr-namespace:DynamicTargetLineSample"
7+
xmlns:chart="clr-namespace:Syncfusion.UI.Xaml.Charts;assembly=Syncfusion.SfChart.WPF"
8+
mc:Ignorable="d"
9+
Title="MainWindow" Height="450" Width="800">
10+
11+
<Window.DataContext>
12+
<local:ViewModel x:Name="viewModel"/>
13+
</Window.DataContext>
14+
15+
<Grid>
16+
<Grid.ColumnDefinitions>
17+
<ColumnDefinition Width="*"></ColumnDefinition>
18+
<ColumnDefinition Width="200"></ColumnDefinition>
19+
</Grid.ColumnDefinitions>
20+
21+
<chart:SfChart Grid.Column="0">
22+
23+
<chart:SfChart.PrimaryAxis>
24+
<chart:CategoryAxis EdgeLabelsDrawingMode="Fit" ShowGridLines="False" Header="Months"/>
25+
</chart:SfChart.PrimaryAxis>
26+
27+
<chart:SfChart.SecondaryAxis>
28+
<chart:NumericalAxis x:Name="Y_Axis" Minimum="0" Maximum="20000" Interval="5000" ShowGridLines="False" Header="Revenue" LabelFormat="'$'0" PlotOffsetEnd="30"/>
29+
</chart:SfChart.SecondaryAxis>
30+
31+
<chart:SfChart.Annotations>
32+
<chart:HorizontalLineAnnotation Y1="{Binding Y1}"
33+
Stroke="Black"
34+
StrokeThickness="2"
35+
StrokeDashArray="5,2,2"
36+
Text="Target"
37+
FontSize="14"
38+
FontWeight="Bold"
39+
HorizontalTextAlignment="Left"
40+
VerticalTextAlignment="Top">
41+
</chart:HorizontalLineAnnotation>
42+
</chart:SfChart.Annotations>
43+
44+
<chart:ColumnSeries ItemsSource="{Binding Data}"
45+
XBindingPath="Months"
46+
YBindingPath="Revenue"
47+
Palette="Custom"
48+
Opacity="0.7">
49+
<chart:ColumnSeries.ColorModel>
50+
<chart:ChartColorModel>
51+
<chart:ChartColorModel.CustomBrushes>
52+
<SolidColorBrush Color="#FF4500"/>
53+
<SolidColorBrush Color="#1E90FF"/>
54+
<SolidColorBrush Color="#32CD32"/>
55+
<SolidColorBrush Color="#FFD700"/>
56+
<SolidColorBrush Color="#FF1493"/>
57+
<SolidColorBrush Color="#9400D3"/>
58+
<SolidColorBrush Color="#00CED1"/>
59+
</chart:ChartColorModel.CustomBrushes>
60+
</chart:ChartColorModel>
61+
</chart:ColumnSeries.ColorModel>
62+
</chart:ColumnSeries>
63+
64+
</chart:SfChart>
65+
66+
<StackPanel Orientation="Vertical" Margin="10" Grid.Column="1">
67+
<TextBlock Text="Adjust Target Line" FontSize="16" FontWeight="Bold" TextAlignment="Center" HorizontalAlignment="Center" Margin="0,0,0,20"/>
68+
<TextBox Text="{Binding Y1}" HorizontalAlignment="Stretch" VerticalAlignment="Center" TextChanged="TextBox_TextChanged" Margin="0,0,0,20" Padding="10"/>
69+
<Slider Minimum="{Binding Minimum, Source={x:Reference Y_Axis}}"
70+
Maximum="{Binding Maximum, Source={x:Reference Y_Axis}}"
71+
Value="{Binding Y1}" HorizontalAlignment="Stretch"/>
72+
</StackPanel>
73+
</Grid>
74+
</Window>
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,64 @@
1+
using System.Text;
2+
using System.Windows;
3+
using System.Windows.Controls;
4+
using System.Windows.Data;
5+
using System.Windows.Documents;
6+
using System.Windows.Input;
7+
using System.Windows.Media;
8+
using System.Windows.Media.Imaging;
9+
using System.Windows.Navigation;
10+
using System.Windows.Shapes;
11+
12+
namespace DynamicTargetLineSample
13+
{
14+
/// <summary>
15+
/// Interaction logic for MainWindow.xaml
16+
/// </summary>
17+
public partial class MainWindow : Window
18+
{
19+
private string _previousValidText = string.Empty;
20+
public MainWindow()
21+
{
22+
InitializeComponent();
23+
}
24+
25+
private void TextBox_TextChanged(object sender, TextChangedEventArgs e)
26+
{
27+
if (Y_Axis == null) return;
28+
var maxValue = Y_Axis.Maximum;
29+
30+
if (sender is TextBox textBox)
31+
{
32+
textBox.TextChanged -= TextBox_TextChanged;
33+
34+
if (string.IsNullOrWhiteSpace(textBox.Text))
35+
{
36+
viewModel.Y1 = double.MinValue;
37+
textBox.Text = string.Empty;
38+
}
39+
else
40+
{
41+
if (int.TryParse(textBox.Text, out int newValue))
42+
{
43+
if (newValue > maxValue)
44+
newValue = (int)maxValue;
45+
else if (newValue < 0)
46+
newValue = 0;
47+
48+
viewModel.Y1 = newValue;
49+
50+
textBox.Text = newValue.ToString();
51+
textBox.CaretIndex = textBox.Text.Length;
52+
}
53+
else
54+
{
55+
textBox.Text = ((int)viewModel.Y1).ToString();
56+
textBox.CaretIndex = textBox.Text.Length;
57+
}
58+
}
59+
60+
textBox.TextChanged += TextBox_TextChanged;
61+
}
62+
}
63+
}
64+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
using System;
2+
using System.Collections.Generic;
3+
using System.Linq;
4+
using System.Text;
5+
using System.Threading.Tasks;
6+
7+
namespace DynamicTargetLineSample
8+
{
9+
internal class Model
10+
{
11+
public string? Months { get; set; }
12+
public double Revenue { get; set; }
13+
}
14+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
using System;
2+
using System.Collections.Generic;
3+
using System.Collections.ObjectModel;
4+
using System.ComponentModel;
5+
using System.Linq;
6+
using System.Text;
7+
using System.Threading.Tasks;
8+
using System.Windows.Media;
9+
10+
namespace DynamicTargetLineSample
11+
{
12+
internal class ViewModel:INotifyPropertyChanged
13+
{
14+
private double y1;
15+
public double Y1
16+
{
17+
get => y1;
18+
set
19+
{
20+
if(y1 != value)
21+
{
22+
y1 = value;
23+
OnPropertyChanged(nameof(Y1));
24+
}
25+
}
26+
}
27+
28+
public event PropertyChangedEventHandler? PropertyChanged;
29+
30+
protected void OnPropertyChanged(string name)
31+
{
32+
PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(name));
33+
}
34+
35+
public ObservableCollection<Model> Data { get; set; }
36+
37+
public ViewModel()
38+
{
39+
Y1 = 12000;
40+
Data = new ObservableCollection<Model>()
41+
{
42+
new Model() { Months = "January", Revenue = 10000 },
43+
new Model() { Months = "February", Revenue = 13500 },
44+
new Model() { Months = "March", Revenue = 16000 },
45+
new Model() { Months = "April", Revenue = 14000 },
46+
new Model() { Months = "May", Revenue = 12500 },
47+
new Model() { Months = "June", Revenue = 18000 },
48+
new Model() { Months = "July", Revenue = 11700 }
49+
};
50+
}
51+
}
52+
}

0 commit comments

Comments
 (0)