Đồ thị (hết)

5. Legend:


public class Legend

    {

        private bool isLegend;

        private bool isBorder;

        private Canvas legendCanvas;



        public Legend()

        {

            isLegend = false;

            isBorder = true;

        }



        public Canvas LegendCanvas

        {

            get { return legendCanvas; }

            set { legendCanvas = value; }

        }



        public bool IsLegend

        {

            get { return isLegend; }

            set { isLegend = value; }

        }



        public bool IsBorder

        {

            get { return isBorder; }

            set { isBorder = value; }

        }



        public void AddLegend(ChartStyleGridlines cs, DataCollection dc)

        {

            TextBlock tb = new TextBlock();

            if (dc.DataList.Count < 1 || !IsLegend)

                return;

            int n = 0;

            string[] legendLabels = new string[dc.DataList.Count];

            foreach (DataSeries ds in dc.DataList)

            {

                legendLabels[n] = ds.SeriesName;

                n++;

            }



            double legendWidth = 0;

            Size size = new Size(0, 0);

            int i;

            for (i = 0; i < legendLabels.Length; i++)

            {

                tb = new TextBlock();

                tb.Text = legendLabels[i];

                tb.Measure(new Size(Double.PositiveInfinity, Double.PositiveInfinity));

                size = tb.DesiredSize;

                if (legendWidth < size.Width)

                    legendWidth = size.Width;

            }



            legendWidth += 50;

            LegendCanvas.Width = legendWidth + 5;

            double legendHeight = 17 * dc.DataList.Count;

            double sx = 6;

            double sy = 2;

            double textHeight = size.Height;

            double lineLength = 34;

            Rectangle legendRect = new Rectangle();

            legendRect.Stroke = Brushes.Black;

            legendRect.Width = legendWidth;

            legendRect.Height = legendHeight;



            if (IsLegend && IsBorder)

            {

                LegendCanvas.Children.Add(legendRect);

            }

            n = 1;

            foreach (DataSeries ds in dc.DataList)

            {

                double xSymbol = sx + lineLength / 2;

                double xText = sx * 2 + lineLength;

                double yText = sy * n + (2 * n – 1) * textHeight / 2;

                Line line = new Line();

                AddLinePattern(line, ds);

                line.X1 = sx;

                line.Y1 = yText;

                line.X2 = sx + lineLength;

                line.Y2 = yText;

                legendCanvas.Children.Add(line);



                tb = new TextBlock();

                tb.Text = ds.SeriesName;

                legendCanvas.Children.Add(tb);

                Canvas.SetTop(tb, yText – size.Height / 2);

                Canvas.SetLeft(tb, xText);

                n++;

            }

        }



        private void AddLinePattern(Line line, DataSeries ds)

        {

            line.Stroke = ds.LineColor;

            line.StrokeThickness = ds.LineThickness;



            switch (ds.LinePattern)

            {

                case DataSeries.LinePatternEnum.Dash:

                    line.StrokeDashArray = new DoubleCollection(new double[2] {4, 3});

                    break;

                case DataSeries.LinePatternEnum.Dot:

                    line.StrokeDashArray = new DoubleCollection(new double[2] { 1, 2 });

                    break;

                case DataSeries.LinePatternEnum.DashDot:

                    line.StrokeDashArray = new DoubleCollection(new double[4] { 4, 2, 1, 2 });

                    break;

                case DataSeries.LinePatternEnum.None:

                    line.Stroke = Brushes.Transparent;

                    break;

            }

        }

    }


File XAML chúng ta sẽ design lại như sau:


<Window x:Class="FirstWPFApp.MainWindow"

        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation&quot;

        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml&quot;

        Title="Do thi" Height="400" Width="500">

    <Viewbox Stretch="Fill">

        <Grid Name="grid1" Margin="10">

            <Grid.ColumnDefinitions>

                <ColumnDefinition Width="Auto" />

                <ColumnDefinition Name="column1" Width="*" />

                <ColumnDefinition Width="Auto" />

            </Grid.ColumnDefinitions>

            <Grid.RowDefinitions>

                <RowDefinition Height="Auto" />

                <RowDefinition Name="row1" Height="*" />

                <RowDefinition Height="Auto" />

            </Grid.RowDefinitions>

            <TextBlock
Margin="2" x:Name="tbTitle" Grid.Column="1" Grid.Row="0"
RenderTransformOrigin="0.5,0.5" FontSize="14" FontWeight="Bold"
HorizontalAlignment="Stretch" VerticalAlignment="Stretch"
TextAlignment="Center" Text="Title" />


            <TextBlock
Margin="2" x:Name="tbXLabel" Grid.Column="1" Grid.Row="2"
RenderTransformOrigin="0.5,0.5" TextAlignment="Center" Text="X Asis"
/>


            <TextBlock
Margin="2" x:Name="tbYLabel" Grid.Column="0" Grid.Row="1"
RenderTransformOrigin="0.5,0.5" TextAlignment="Center" Text="Y Asis">


                <TextBlock.LayoutTransform>

                    <RotateTransform Angle="-90" />

                </TextBlock.LayoutTransform>

            </TextBlock>

            <Canvas Name="legendCanvas" Margin="0,10" Grid.Column="2" Grid.Row="1" Width="60" Height="Auto" />

            <Canvas
Margin="2" Name="textCanvas" Width="300" Height="305" Grid.Column="1"
Grid.Row="1" ClipToBounds="True">


                <Canvas Name="chartCanvas" ClipToBounds="True" />

            </Canvas>

        </Grid>

    </Viewbox>

</Window>


Xong, chúng ta sẽ code lại file xaml.cs như sau để thực thi:


public partial class MainWindow : Window

    {

        private ChartStyleGridlines cs;

        private Legend lg = new Legend();

        private DataCollection dc = new DataCollection();

        private DataSeries ds = new DataSeries();



        public MainWindow()

        {

            InitializeComponent();

            AddChart();

            lg.LegendCanvas = legendCanvas;

            lg.IsLegend = true;

            lg.IsBorder = true;

            lg.AddLegend(cs, dc);

        }



        private void AddChart()

        {

            cs = new ChartStyleGridlines();

            cs.ChartCanvas = chartCanvas;

            cs.TextCanvas = textCanvas;

            cs.Title = "Do thi sin va cos";

            cs.Xmin = 0;

            cs.Xmax = 7;

            cs.Ymin = -1.5;

            cs.Ymax = 1.5;

            cs.YTick = 0.5;



            cs.GridlinePattern = ChartStyleGridlines.GridlinePatternEnum.Dot;

            cs.GridlineColor = Brushes.Black;

            cs.AddChartStyle(tbTitle, tbXLabel, tbYLabel);



            ds = new DataSeries();

            ds.LineColor = Brushes.Blue;

            ds.SeriesName = "Sin";

            ds.LineThickness = 1;



            int i;

            for(i = 0; i < 70; i++)

            {

                double x = i / 5.0;

                double y = Math.Sin(x);

                ds.LineSeries.Points.Add(new Point(x, y));

            }

            dc.DataList.Add(ds);



            ds = new DataSeries();

            ds.LineColor = Brushes.Red;

            ds.SeriesName = "Cos";

            ds.LinePattern = DataSeries.LinePatternEnum.DashDot;

            ds.LineThickness = 2;

            for (i = 0; i < 70; i++)

            {

                double x = i / 5.0;

                double y = Math.Cos(x);

                ds.LineSeries.Points.Add(new Point(x, y));

            }

            dc.DataList.Add(ds);



            ds = new DataSeries();

            ds.LineColor = Brushes.Green;

            ds.SeriesName = "Sin^2";

            ds.LinePattern = DataSeries.LinePatternEnum.Dot;

            ds.LineThickness = 2;

            for (i = 0; i < 70; i++)

            {

                double x = i / 5.0;

                double y = Math.Sin(x) * Math.Sin(x);

                ds.LineSeries.Points.Add(new Point(x, y));

            }

            dc.DataList.Add(ds);



            dc.AddLines(chartCanvas, cs);

        }

    }


Xong, bây giờ các bạn nhấn F5 để build chương trình và nhận được kết quả:

Qua ví dụ trên chúng ta đã phần nào thấy được khả năng hỗ trợ đồ họa cực kì tốt của WPF, nhớ lại lúc thực hiện với Windows Form thực sự đây là một vấn đề không nhỏ ?_?

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s