股票图形定制轴在silverlight中的实现---涨跌幅Y轴(又称百分比轴)

如上图所示,左侧Y轴通过定制化后最终可以转换成为右侧Y轴的效果,同通达信的做法一致。实现的知识点涉及到RelativeSource和转换器的使用,如何实现代码如下:

<UserControl x:Class="SilverlightApplication1.MainPage"
    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"
    mc:Ignorable="d"
    d:DesignHeight="300" d:DesignWidth="400" xmlns:toolkit="http://schemas.microsoft.com/winfx/2006/xaml/presentation/toolkit"
    xmlns:local="clr-namespace:SilverlightApplication1"
    >
 <UserControl.Resources>
        <local:DataConvert x:Key="DataConvert"/>
        <local:DataConvert2 x:Key="DataConvert2"/>
     <Style x:Key="NumericAxisLabelStyle1" TargetType="toolkit:NumericAxisLabel">
      <Setter Property="IsTabStop" Value="False"/>
            <!--<Setter Property="StringFormat" Value="{}{0:P}"/>-->
      <Setter Property="Template">
       <Setter.Value>
        <ControlTemplate TargetType="toolkit:NumericAxisLabel">
                        <!--<TextBlock Text="{TemplateBinding FormattedContent}"/>-->
                        <TextBlock Text="{Binding Path=FormattedContent,Converter={StaticResource DataConvert}, RelativeSource={RelativeSource TemplatedParent}}"
                                   Foreground="{Binding Path=FormattedContent,Converter={StaticResource DataConvert2}, RelativeSource={RelativeSource TemplatedParent}}"/>
                    </ControlTemplate>
       </Setter.Value>
      </Setter>
     </Style>
 </UserControl.Resources>

    <Grid x:Name="LayoutRoot" Background="White">
        <toolkit:Chart Name="chart1" Title="万科A股(涨跌幅Y轴)" VerticalAlignment="Stretch" HorizontalAlignment="Stretch">
            <toolkit:Chart.Axes>
                <toolkit:LinearAxis Orientation="Y" Location="Right" Minimum="6.81" Maximum="7.74" ShowGridLines="True" AxisLabelStyle="{StaticResource NumericAxisLabelStyle1}"/>
            </toolkit:Chart.Axes>
            <toolkit:Chart.Series>
               
                <toolkit:LineSeries DependentValuePath="Y" IndependentValuePath="X" Title="分时走势">
                    <toolkit:LineSeries.ItemsSource>
                        <PointCollection>
                            <Point>1,7.26</Point>
                            <Point>2,7.20</Point>
                            <Point>3,7.29</Point>
                            <Point>4,7.31</Point>
                            <Point>5,7.36</Point>
                            <Point>6,7.42</Point>
                            <Point>7,7.50</Point>
                            <Point>8,7.42</Point>
                        </PointCollection>
                    </toolkit:LineSeries.ItemsSource>
                    <toolkit:LineSeries.DependentRangeAxis>
                        <toolkit:LinearAxis Orientation="Y" Location="Left" Minimum="6.81" Maximum="7.74"/>
                    </toolkit:LineSeries.DependentRangeAxis>
                </toolkit:LineSeries>

                <toolkit:LineSeries DependentValuePath="Y" IndependentValuePath="X" Title="昨收盘线">
                    <toolkit:LineSeries.ItemsSource>
                        <PointCollection>
                            <Point>1,7.24</Point><!--昨收盘为:7.24-->
                            <Point>8,7.24</Point>
                        </PointCollection>
                    </toolkit:LineSeries.ItemsSource>
                </toolkit:LineSeries>

            </toolkit:Chart.Series>
        </toolkit:Chart>
    </Grid>
</UserControl>

using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using System.Windows.Data;
using System.Globalization;

namespace SilverlightApplication1
{
    public partial class MainPage : UserControl
    {
        public MainPage()
        {
            InitializeComponent();
        }
    }

    public class DataConvert : IValueConverter
    {
        public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
        {
            double tmp = double.Parse(value.ToString());
            tmp = (tmp - 7.24) / 7.24*100;//2010年7月12日的昨收盘数据为7.24
            return ((tmp).ToString("f2")+"%").Replace("-","");
        }

        public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
        {
            return value;
        }
    }

    public class DataConvert2 : IValueConverter
    {
        public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
        {
            double tmp = double.Parse(value.ToString());
            tmp = (tmp - 7.24) / 7.24*100;//2010年7月12日的昨收盘数据为7.24
            if (tmp > 0)
                return "Red";
            else
                return "Green";
        }

        public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
        {
            return value;
        }
    }
}

注:x轴经过定制,也可以通达信的效果,由于已经实现了,所以,在此略过。

posted on 2010-07-12 14:41  chuncn  阅读(1319)  评论(0编辑  收藏  举报

导航