public class CustomBarSeries: BarSeries
{
private Storyboard sb;
public void Animate()
{
int i = 0;
if (sb != null)
sb.Stop();
sb = new Storyboard();
string path = !IsTransposed ?
"(UIElement.RenderTransform).(ScaleTransform.ScaleX)" :
"(UIElement.RenderTransform).(ScaleTransform.ScaleY)";
string adornTransPath = !IsTransposed ?
"(UIElement.RenderTransform).(TranslateTransform.X)" :
"(UIElement.RenderTransform).(TranslateTransform.Y)";
foreach (ChartSegment segment in Segments)
{
double elementSize = 0d;
var element = (FrameworkElement)segment.GetRenderedVisual();
if (segment is EmptyPointSegment && (!(EmptyPointStyle ==
EmptyPointStyle.Interior) ||
EmptyPointStyle == EmptyPointStyle.SymbolAndInterior))
elementSize = !IsTransposed ?
((EmptyPointSegment)segment).EmptyPointSymbolWidth :
((EmptyPointSegment)segment).EmptyPointSymbolHeight;
else
elementSize = !IsTransposed ? ((BarSegment)segment).XData :
((BarSegment)segment).YData;
if (!double.IsNaN(elementSize) && !double.IsNaN(YValues[i]))
{
element.RenderTransform = new ScaleTransform();
if (YValues[i] < 0 && !IsTransposed)
element.RenderTransformOrigin = new Point(1, 1);
else if (YValues[i] > 0 && IsTransposed)
element.RenderTransformOrigin = new Point(1, 1);
DoubleAnimationUsingKeyFrames keyFrames1 = new
DoubleAnimationUsingKeyFrames();
SplineDoubleKeyFrame keyFrame1 = new SplineDoubleKeyFrame();
keyFrame1.KeyTime = KeyTime.FromTimeSpan(TimeSpan.FromSeconds(0));
keyFrame1.Value = 0;
keyFrames1.KeyFrames.Add(keyFrame1);
keyFrame1 = new SplineDoubleKeyFrame();
keyFrame1.KeyTime = KeyTime.FromTimeSpan(AnimationDuration);
KeySpline keySpline1 = new KeySpline();
keySpline1.ControlPoint1 = new Point(0.64, 0.84);
keySpline1.ControlPoint2 = new Point(0, 1);
keyFrame1.KeySpline = keySpline1;
keyFrames1.KeyFrames.Add(keyFrame1);
keyFrame1.Value = 1;
keyFrames1.EnableDependentAnimation = true;
Storyboard.SetTargetProperty(keyFrames1, path);
Storyboard.SetTarget(keyFrames1, element);
sb.Children.Add(keyFrames1);
}
i++;
}
sb.Begin();
}
} |
<Grid >
<Grid.DataContext>
<local:ViewModel/>
</Grid.DataContext>
<Grid.RowDefinitions>
<RowDefinition/>
<RowDefinition Height="100"/>
</Grid.RowDefinitions>
<chart:SfChart Margin="10" x:Name="chart">
<chart:SfChart.PrimaryAxis>
<chart:CategoryAxis/>
</chart:SfChart.PrimaryAxis>
<chart:SfChart.SecondaryAxis>
<chart:NumericalAxis/>
</chart:SfChart.SecondaryAxis>
<local:CustomBarSeries
EnableAnimation="True"
x:Name="bar">
</local:CustomBarSeries>
</chart:SfChart>
<Button Click="add_Click" Grid.Row="1"
Content="Add Data"/>
</Grid> |
private async void add_Click(object sender, RoutedEventArgs e)
{
var count = (bar.ItemsSource as ObservableCollection<Model>).Count - 1;
var itemsSource = bar.ItemsSource as ObservableCollection<Model>;
itemsSource.Add(new Model() { XValue = itemsSource[count].XValue + 1,
YValue = rd.Next(20, 45) });
if (bar.EnableAnimation)
await Dispatcher.RunAsync(CoreDispatcherPriority.Normal, bar.Animate);
} |