Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Attach the Dynamic Target Line WPF Chart KB sample #1

Merged
merged 13 commits into from
Jan 15, 2025
22 changes: 22 additions & 0 deletions DynamicTargetLineSample/DynamicTargetLineSample.sln
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@

Microsoft Visual Studio Solution File, Format Version 12.00
# Visual Studio Version 17
VisualStudioVersion = 17.12.35506.116 d17.12
MinimumVisualStudioVersion = 10.0.40219.1
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "DynamicTargetLineSample", "DynamicTargetLineSample\DynamicTargetLineSample.csproj", "{59A4FB8C-77CC-40EF-87C4-4ECA9BBE9F8E}"
EndProject
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution
Debug|Any CPU = Debug|Any CPU
Release|Any CPU = Release|Any CPU
EndGlobalSection
GlobalSection(ProjectConfigurationPlatforms) = postSolution
{59A4FB8C-77CC-40EF-87C4-4ECA9BBE9F8E}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{59A4FB8C-77CC-40EF-87C4-4ECA9BBE9F8E}.Debug|Any CPU.Build.0 = Debug|Any CPU
{59A4FB8C-77CC-40EF-87C4-4ECA9BBE9F8E}.Release|Any CPU.ActiveCfg = Release|Any CPU
{59A4FB8C-77CC-40EF-87C4-4ECA9BBE9F8E}.Release|Any CPU.Build.0 = Release|Any CPU
EndGlobalSection
GlobalSection(SolutionProperties) = preSolution
HideSolutionNode = FALSE
EndGlobalSection
EndGlobal
9 changes: 9 additions & 0 deletions DynamicTargetLineSample/DynamicTargetLineSample/App.xaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
<Application x:Class="DynamicTargetLineSample.App"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:local="clr-namespace:DynamicTargetLineSample"
StartupUri="MainWindow.xaml">
<Application.Resources>

</Application.Resources>
</Application>
14 changes: 14 additions & 0 deletions DynamicTargetLineSample/DynamicTargetLineSample/App.xaml.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
using System.Configuration;
using System.Data;
using System.Windows;

namespace DynamicTargetLineSample
{
/// <summary>
/// Interaction logic for App.xaml
/// </summary>
public partial class App : Application
{
}

}
10 changes: 10 additions & 0 deletions DynamicTargetLineSample/DynamicTargetLineSample/AssemblyInfo.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
using System.Windows;

[assembly: ThemeInfo(
ResourceDictionaryLocation.None, //where theme specific resource dictionaries are located
//(used if a resource is not found in the page,
// or application resource dictionaries)
ResourceDictionaryLocation.SourceAssembly //where the generic resource dictionary is located
//(used if a resource is not found in the page,
// app, or any theme specific resource dictionaries)
)]
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<OutputType>WinExe</OutputType>
<TargetFramework>net9.0-windows</TargetFramework>
<Nullable>enable</Nullable>
<ImplicitUsings>enable</ImplicitUsings>
<UseWPF>true</UseWPF>
</PropertyGroup>

<ItemGroup>
<PackageReference Include="Syncfusion.SfChart.WPF" Version="*" />
</ItemGroup>

</Project>
74 changes: 74 additions & 0 deletions DynamicTargetLineSample/DynamicTargetLineSample/MainWindow.xaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,74 @@
<Window x:Class="DynamicTargetLineSample.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:local="clr-namespace:DynamicTargetLineSample"
xmlns:chart="clr-namespace:Syncfusion.UI.Xaml.Charts;assembly=Syncfusion.SfChart.WPF"
mc:Ignorable="d"
Title="MainWindow" Height="450" Width="800">

<Window.DataContext>
<local:ViewModel x:Name="viewModel"/>
</Window.DataContext>

<Grid>
<Grid.ColumnDefinitions>
<ColumnDefinition Width="*"></ColumnDefinition>
<ColumnDefinition Width="200"></ColumnDefinition>
</Grid.ColumnDefinitions>

<chart:SfChart Grid.Column="0">

<chart:SfChart.PrimaryAxis>
<chart:CategoryAxis EdgeLabelsDrawingMode="Fit" ShowGridLines="False" Header="Months"/>
</chart:SfChart.PrimaryAxis>

<chart:SfChart.SecondaryAxis>
<chart:NumericalAxis x:Name="Y_Axis" Minimum="0" Maximum="20000" Interval="5000" ShowGridLines="False" Header="Revenue" LabelFormat="'$'0" PlotOffsetEnd="30"/>
</chart:SfChart.SecondaryAxis>

<chart:SfChart.Annotations>
<chart:HorizontalLineAnnotation Y1="{Binding Y1}"
Stroke="Black"
StrokeThickness="2"
StrokeDashArray="5,2,2"
Text="Target"
FontSize="14"
FontWeight="Bold"
HorizontalTextAlignment="Left"
VerticalTextAlignment="Top">
</chart:HorizontalLineAnnotation>
</chart:SfChart.Annotations>

<chart:ColumnSeries ItemsSource="{Binding Data}"
XBindingPath="Months"
YBindingPath="Revenue"
Palette="Custom"
Opacity="0.7">
<chart:ColumnSeries.ColorModel>
<chart:ChartColorModel>
<chart:ChartColorModel.CustomBrushes>
<SolidColorBrush Color="#FF4500"/>
<SolidColorBrush Color="#1E90FF"/>
<SolidColorBrush Color="#32CD32"/>
<SolidColorBrush Color="#FFD700"/>
<SolidColorBrush Color="#FF1493"/>
<SolidColorBrush Color="#9400D3"/>
<SolidColorBrush Color="#00CED1"/>
</chart:ChartColorModel.CustomBrushes>
</chart:ChartColorModel>
</chart:ColumnSeries.ColorModel>
</chart:ColumnSeries>

</chart:SfChart>

<StackPanel Orientation="Vertical" Margin="10" Grid.Column="1">
<TextBlock Text="Adjust Target Line" FontSize="16" FontWeight="Bold" TextAlignment="Center" HorizontalAlignment="Center" Margin="0,0,0,20"/>
<TextBox Text="{Binding Y1}" HorizontalAlignment="Stretch" VerticalAlignment="Center" TextChanged="TextBox_TextChanged" Margin="0,0,0,20" Padding="10"/>
<Slider Minimum="{Binding Minimum, Source={x:Reference Y_Axis}}"
Maximum="{Binding Maximum, Source={x:Reference Y_Axis}}"
Value="{Binding Y1}" HorizontalAlignment="Stretch"/>
</StackPanel>
</Grid>
</Window>
64 changes: 64 additions & 0 deletions DynamicTargetLineSample/DynamicTargetLineSample/MainWindow.xaml.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;

namespace DynamicTargetLineSample
{
/// <summary>
/// Interaction logic for MainWindow.xaml
/// </summary>
public partial class MainWindow : Window
{
private string _previousValidText = string.Empty;
public MainWindow()
{
InitializeComponent();
}

private void TextBox_TextChanged(object sender, TextChangedEventArgs e)
{
if (Y_Axis == null) return;
var maxValue = Y_Axis.Maximum;

if (sender is TextBox textBox)
{
textBox.TextChanged -= TextBox_TextChanged;

if (string.IsNullOrWhiteSpace(textBox.Text))
{
viewModel.Y1 = double.MinValue;
textBox.Text = string.Empty;
}
else
{
if (int.TryParse(textBox.Text, out int newValue))
{
if (newValue > maxValue)
newValue = (int)maxValue;
else if (newValue < 0)
newValue = 0;

viewModel.Y1 = newValue;

textBox.Text = newValue.ToString();
textBox.CaretIndex = textBox.Text.Length;
}
else
{
textBox.Text = ((int)viewModel.Y1).ToString();
textBox.CaretIndex = textBox.Text.Length;
}
}

textBox.TextChanged += TextBox_TextChanged;
}
}
}
}
14 changes: 14 additions & 0 deletions DynamicTargetLineSample/DynamicTargetLineSample/Model.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace DynamicTargetLineSample
{
internal class Model
{
public string? Months { get; set; }
public double Revenue { get; set; }
}
}
52 changes: 52 additions & 0 deletions DynamicTargetLineSample/DynamicTargetLineSample/ViewModel.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.ComponentModel;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Media;

namespace DynamicTargetLineSample
{
internal class ViewModel:INotifyPropertyChanged
{
private double y1;
public double Y1
{
get => y1;
set
{
if(y1 != value)
{
y1 = value;
OnPropertyChanged(nameof(Y1));
}
}
}

public event PropertyChangedEventHandler? PropertyChanged;

protected void OnPropertyChanged(string name)
{
PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(name));
}

public ObservableCollection<Model> Data { get; set; }

public ViewModel()
{
Y1 = 12000;
Data = new ObservableCollection<Model>()
{
new Model() { Months = "January", Revenue = 10000 },
new Model() { Months = "February", Revenue = 13500 },
new Model() { Months = "March", Revenue = 16000 },
new Model() { Months = "April", Revenue = 14000 },
new Model() { Months = "May", Revenue = 12500 },
new Model() { Months = "June", Revenue = 18000 },
new Model() { Months = "July", Revenue = 11700 }
};
}
}
}
Loading