WPF編程,Live Charts使用說明(45)——百分比堆積行

在這裏插入圖片描述

後臺:

using System;
using System.Windows.Controls;
using LiveCharts;
using LiveCharts.Wpf;
 
namespace Wpf.CartesianChart.Basic_Stacked_Bar
{
    /// <summary>
    /// Interaction logic for BasicStackedRowPercentageExample.xaml
    /// </summary>
    public partial class BasicStackedRowPercentageExample : UserControl
    {
        public BasicStackedRowPercentageExample()
        {
            InitializeComponent();
 
            SeriesCollection = new SeriesCollection
            {
                new StackedRowSeries
                {
                    Values = new ChartValues<double> {4, 5, 6, 8},
                    StackMode = StackMode.Percentage,
                    DataLabels = true,
                    LabelPoint = p => p.X.ToString()
                },
                new StackedRowSeries
                {
                    Values = new ChartValues<double> {2, 5, 6, 7},
                    StackMode = StackMode.Percentage,
                    DataLabels = true,
                    LabelPoint = p => p.X.ToString()
                }
            };
 
            //adding series updates and animates the chart
            SeriesCollection.Add(new StackedRowSeries
            {
                Values = new ChartValues<double> { 6, 2, 7 },
                StackMode = StackMode.Percentage,
                DataLabels = true,
                LabelPoint = p => p.X.ToString()
            });
 
            //adding values also updates and animates
            SeriesCollection[2].Values.Add(4d);
 
            Labels = new[] { "Chrome", "Mozilla", "Opera", "IE" };
            Formatter = val => val.ToString("P");
 
            DataContext = this;
        }
 
        public SeriesCollection SeriesCollection { get; set; }
        public string[] Labels { get; set; }
        public Func<double, string> Formatter { get; set; }
 
    }
}

前臺:

<UserControl x:Class="Wpf.CartesianChart.Basic_Stacked_Bar.BasicStackedRowPercentageExample"
             xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
             xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
             xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" 
             xmlns:d="http://schemas.microsoft.com/expression/blend/2008" 
             xmlns:local="clr-namespace:Wpf.CartesianChart.Basic_Stacked_Bar"
             xmlns:lvc="clr-namespace:LiveCharts.Wpf;assembly=LiveCharts.Wpf"
             mc:Ignorable="d" 
             d:DesignHeight="300" d:DesignWidth="300">
    <Grid>
        <lvc:CartesianChart Series="{Binding SeriesCollection}" LegendLocation="Top">
            <lvc:CartesianChart.AxisY>
                <lvc:Axis Title="Browser" Labels="{Binding Labels}" />
            </lvc:CartesianChart.AxisY>
            <lvc:CartesianChart.AxisX>
                <lvc:Axis LabelFormatter="{Binding Formatter}" />
            </lvc:CartesianChart.AxisX>
            <lvc:CartesianChart.DataTooltip>
                <lvc:DefaultTooltip SelectionMode="SharedYValues"></lvc:DefaultTooltip>
            </lvc:CartesianChart.DataTooltip>
        </lvc:CartesianChart>
    </Grid>
</UserControl>
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章