WPF 自定義BarChartControl(可左右滑動的柱狀圖)

来源:http://www.cnblogs.com/kybs0/archive/2016/09/04/5840150.html
-Advertisement-
Play Games

自定義可左右滑動、拖拽滑動的平面柱狀圖 在做這種樣式控制項之前,可先瀏覽我之前預研的控制項: A、自定義左右滑動ScrollViewer(可拖動滑動) B、自定義Bar柱狀圖 OK,現在說下控制項具體設計過程: 1)採用Grid佈局,這樣可以將Y軸的標題設置平均高度,X軸的柱子也可以平均。 當然X軸也會存 ...


自定義可左右滑動、拖拽滑動的平面柱狀圖

在做這種樣式控制項之前,可先瀏覽我之前預研的控制項:

A、自定義左右滑動ScrollViewer(可拖動滑動)

B、自定義Bar柱狀圖

 OK,現在說下控制項具體設計過程:

1)採用Grid佈局,這樣可以將Y軸的標題設置平均高度,X軸的柱子也可以平均。

  當然X軸也會存在一個問題,當數據較少時,只有倆個柱子難道就佈滿界面?

  很簡單,在Grid中多添加一個ColumnDefinition就行了,柱子的寬度設置成可配置,額外的一個設置成預設填充。

2)Bar採用RadioButton,因為有Checked屬性,之後整個控制項SelectionChanged的事件源可以從這來

3)滑動條容器,我這邊比較熟悉DevExpress控制項,所以用的是ScrollBox,然後修改其中的模板。

其它的一些設置比較簡單,就不說了,直接看代碼

1、UserControl界面

<UserControl x:Class="BarChartControlDemo.BarChartControl"
            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:dxlc="http://schemas.devexpress.com/winfx/2008/xaml/layoutcontrol"
            mc:Ignorable="d" 
            d:DesignHeight="100" d:DesignWidth="200" Loaded="BarChartControl_OnLoaded">
    <UserControl.Resources>
        <Style x:Key="ScrollBarStyle1" TargetType="{x:Type ScrollBar}">
            <Setter Property="Template">
                <Setter.Value>
                    <ControlTemplate TargetType="{x:Type ScrollBar}">
                        <Grid Height="0"></Grid>
                    </ControlTemplate>
                </Setter.Value>
            </Setter>
        </Style>
        <ControlTemplate x:Key="LightedBtnTemplate" TargetType="RadioButton">
            <Grid>
                <Grid.RowDefinitions>
                    <RowDefinition Height="5"></RowDefinition>
                    <RowDefinition></RowDefinition>
                    <RowDefinition Height="5"></RowDefinition>
                </Grid.RowDefinitions>
                <Grid.ColumnDefinitions>
                    <ColumnDefinition Width="5"></ColumnDefinition>
                    <ColumnDefinition></ColumnDefinition>
                    <ColumnDefinition Width="5"></ColumnDefinition>
                </Grid.ColumnDefinitions>
                <Rectangle Grid.Row="1" Grid.Column="1" Fill="{TemplateBinding Background}"></Rectangle>

                <Grid x:Name="Grid_00" Grid.Row="0" Grid.Column="0"></Grid>
                <Grid x:Name="Grid_02" Grid.Row="0" Grid.Column="2"></Grid>
                <Grid x:Name="Grid_20" Grid.Row="2" Grid.Column="0"></Grid>
                <Grid x:Name="Grid_22" Grid.Row="2" Grid.Column="2"></Grid>
                <Grid x:Name="Grid_Top" Grid.Row="0" Grid.Column="1"></Grid>
                <Grid x:Name="Grid_Bottom" Grid.Row="2" Grid.Column="1"></Grid>
                <Grid x:Name="Grid_Left" Grid.Row="1" Grid.Column="0"></Grid>
                <Grid x:Name="Grid_Right" Grid.Row="1" Grid.Column="2"></Grid>
            </Grid>
            <ControlTemplate.Triggers>
                <Trigger Property="IsChecked" Value="True">
                    <Setter TargetName="Grid_00" Property="Background">
                        <Setter.Value>
                            <LinearGradientBrush StartPoint="1,1" EndPoint="0,0">
                                <GradientStop Color="White" Offset="0"></GradientStop>
                                <GradientStop Color="Transparent" Offset="0.5"></GradientStop>
                            </LinearGradientBrush>
                        </Setter.Value>
                    </Setter>
                    <Setter TargetName="Grid_02" Property="Background">
                        <Setter.Value>
                            <LinearGradientBrush StartPoint="0,1" EndPoint="1,0">
                                <GradientStop Color="White" Offset="0"></GradientStop>
                                <GradientStop Color="Transparent" Offset="0.5"></GradientStop>
                            </LinearGradientBrush>

                        </Setter.Value>
                    </Setter>
                    <Setter TargetName="Grid_20" Property="Background">
                        <Setter.Value>
                            <LinearGradientBrush StartPoint="1,0" EndPoint="0,1">
                                <GradientStop Color="White" Offset="0"></GradientStop>
                                <GradientStop Color="Transparent" Offset="0.5"></GradientStop>
                            </LinearGradientBrush>
                        </Setter.Value>
                    </Setter>
                    <Setter TargetName="Grid_22" Property="Background">
                        <Setter.Value>
                            <LinearGradientBrush StartPoint="0,0" EndPoint="1,1">
                                <GradientStop Color="White" Offset="0"></GradientStop>
                                <GradientStop Color="Transparent" Offset="0.5"></GradientStop>
                            </LinearGradientBrush>
                        </Setter.Value>
                    </Setter>
                    <Setter TargetName="Grid_Top" Property="Background">
                        <Setter.Value>
                            <LinearGradientBrush StartPoint="1,1" EndPoint="1,0">
                                <GradientStop Color="White" Offset="0"></GradientStop>
                                <GradientStop Color="Transparent" Offset="1"></GradientStop>
                            </LinearGradientBrush>
                        </Setter.Value>
                    </Setter>
                    <Setter TargetName="Grid_Bottom" Property="Background">
                        <Setter.Value>
                            <LinearGradientBrush StartPoint="1,0" EndPoint="1,1">
                                <GradientStop Color="White" Offset="0"></GradientStop>
                                <GradientStop Color="Transparent" Offset="1"></GradientStop>
                            </LinearGradientBrush>
                        </Setter.Value>
                    </Setter>
                    <Setter TargetName="Grid_Left" Property="Background">
                        <Setter.Value>
                            <LinearGradientBrush StartPoint="1,1" EndPoint="0,1">
                                <GradientStop Color="White" Offset="0"></GradientStop>
                                <GradientStop Color="Transparent" Offset="1"></GradientStop>
                            </LinearGradientBrush>
                        </Setter.Value>
                    </Setter>
                    <Setter TargetName="Grid_Right" Property="Background">
                        <Setter.Value>
                            <LinearGradientBrush StartPoint="0,1" EndPoint="1,1">
                                <GradientStop Color="White" Offset="0"></GradientStop>
                                <GradientStop Color="Transparent" Offset="1"></GradientStop>
                            </LinearGradientBrush>
                        </Setter.Value>
                    </Setter>
                </Trigger>
            </ControlTemplate.Triggers>
        </ControlTemplate>
    </UserControl.Resources>
    <Grid>
        <Grid.RowDefinitions>
            <RowDefinition Height="Auto"></RowDefinition>
            <RowDefinition Height="*"></RowDefinition>
            <RowDefinition Height="Auto"></RowDefinition>
        </Grid.RowDefinitions>
        <Grid.ColumnDefinitions>
            <ColumnDefinition Width="Auto"></ColumnDefinition>
            <ColumnDefinition Width="*"></ColumnDefinition>
            <ColumnDefinition Width="Auto"></ColumnDefinition>
        </Grid.ColumnDefinitions>
        <Grid x:Name="HeaderGrid" Grid.Row="0" Grid.Column="1" Height="20">
        </Grid>

        <Grid x:Name="LeftGrid" Grid.Row="1" Grid.Column="0" Width="20">
        </Grid>
        <Border x:Name="MainBorder" Grid.Row="1" Grid.Column="1" BorderThickness="2,0,2,0" BorderBrush="Black">
        </Border>
        <Grid x:Name="MainGridForRow1" Grid.Row="1" Grid.Column="1">

        </Grid>

        <Grid x:Name="MainGridFrom0To1Content" Grid.Row="0" Grid.RowSpan="3" Grid.Column="1">
            <dxlc:ScrollBox x:Name="MyScrollBoxFrom0To1" HorizontalScrollBarStyle="{StaticResource ScrollBarStyle1}" Padding="0" Margin="0">
                <Grid x:Name="MainBarContent" VerticalAlignment="Stretch">

                </Grid>
            </dxlc:ScrollBox>
            <Button x:Name="BtnLeft" Height="30" Margin="5" Opacity="0.5" Click="BtnLeft_OnClick" Background="Transparent" Content="向左滑" HorizontalAlignment="Left" Visibility="Visible">
                <Button.Template>
                    <ControlTemplate>
                        <Grid VerticalAlignment="Center">
                            <Path Stroke="White" StrokeThickness="3" Data="m0,13 L13,0"></Path>
                            <Path Stroke="White" StrokeThickness="3" Data="m0,11 L13,24"></Path>
                        </Grid>
                    </ControlTemplate>
                </Button.Template>
            </Button>
            <Button x:Name="BtnRight" Height="25" Margin="5" Click="BtnRight_OnClick" Background="Transparent" Content="向右滑" HorizontalAlignment="Right" Visibility="Visible">
                <Button.Template>
                    <ControlTemplate>
                        <Grid VerticalAlignment="Center">
                            <Path Stroke="White" StrokeThickness="3" Data="m13,13 L0,0"></Path>
                            <Path Stroke="White" StrokeThickness="3" Data="m13,11 L0,24"></Path>
                        </Grid>
                    </ControlTemplate>
                </Button.Template>
            </Button>
        </Grid>

        <!--<Grid x:Name="RightGrid" Grid.Row="1" Grid.Column="2">
            </Grid>-->

        <Grid x:Name="BottomGrid" Grid.Row="2" Grid.Column="1" Height="20">
        </Grid>
    </Grid>
</UserControl>
View Code

2、UserControl後臺

 public partial class BarChartControl : UserControl
    {
        public BarChartControl()
        {
            InitializeComponent();
        }

        public Brush BorderBrush
        {
            get { return (Brush)GetValue(BorderBrushProperty); }
            set { SetValue(BorderBrushProperty, value); }
        }

        public static readonly DependencyProperty BorderBrushProperty = DependencyProperty.Register("BorderBrush",
        typeof(Brush), typeof(BarChartControl),
        new PropertyMetadata(Brushes.Black));

        public Thickness BorderThickness
        {
            get { return (Thickness)GetValue(BorderThicknessProperty); }
            set { SetValue(BorderThicknessProperty, value); }
        }

        public static readonly DependencyProperty BorderThicknessProperty = DependencyProperty.Register("BorderThickness",
        typeof(Thickness), typeof(BarChartControl),
        new PropertyMetadata(new Thickness(1.0, 0.0, 1.0, 1.0)));

        public AxisYModel AxisY
        {
            get { return (AxisYModel)GetValue(AxisYProperty); }
            set { SetValue(AxisYProperty, value); }
        }

        public static readonly DependencyProperty AxisYProperty = DependencyProperty.Register("AxisY",
        typeof(AxisYModel), typeof(BarChartControl),
        new PropertyMetadata(new AxisYModel()));

        public AxisXModel AxisX
        {
            get { return (AxisXModel)GetValue(AxisXProperty); }
            set { SetValue(AxisXProperty, value); }
        }

        public static readonly DependencyProperty AxisXProperty = DependencyProperty.Register("AxisX",
        typeof(AxisXModel), typeof(BarChartControl),
        new PropertyMetadata(new AxisXModel()));
        public double HeaderHeight
        {
            get { return (double)GetValue(HeaderHeightProperty); }
            set { SetValue(HeaderHeightProperty, value); }
        }
        public static readonly DependencyProperty HeaderHeightProperty = DependencyProperty.Register("HeaderHeight",
        typeof(double), typeof(BarChartControl), new PropertyMetadata(10.0));
        public string Header
        {
            get { return (string)GetValue(HeaderProperty); }
            set { SetValue(HeaderProperty, value); }
        }
        public static readonly DependencyProperty HeaderProperty = DependencyProperty.Register("Header",
        typeof(string), typeof(BarChartControl), new PropertyMetadata());

        private void BarChartControl_OnLoaded(object sender, RoutedEventArgs e)
        {
            MainBorder.BorderBrush = BorderBrush;
            MainBorder.BorderThickness = BorderThickness;

            HeaderGrid.Height = HeaderHeight;
            MainBarContent.Height = MyScrollBoxFrom0To1.ActualHeight - 17;
            LeftGrid.Width = AxisY.Width;

            SetYTitlesContent();

            SetXDatasContent();
        }

        private void SetXDatasContent()
        {
            var axisXModel = AxisX;
            if (axisXModel.Datas.Count > 0)
            {
                int count = axisXModel.Datas.Count;
                double barArea = axisXModel.BarWidth + axisXModel.MarginWidth * 2;

                for (int i = 0; i < count + 1; i++)
                {
                    MainBarContent.ColumnDefinitions.Add(new ColumnDefinition()
                    {
                        Width = new GridLength(barArea)
                    });
                }
                int index = 0;
                foreach (var data in axisXModel.Datas)
                {
                    //主內容
                    var stackPanel = new StackPanel();
                    stackPanel.Orientation = Orientation.Vertical;

                    var tbl = new TextBlock();
                    tbl.Height = 15;
                    tbl.Text = data.Value.ToString();
                    tbl.Foreground = axisXModel.ForeGround;
                    tbl.HorizontalAlignment = HorizontalAlignment.Center;
                    stackPanel.Children.Add(tbl);

                    var radioButton = new RadioButton();
                    radioButton.Template = (ControlTemplate)this.Resources["LightedBtnTemplate"];
                    radioButton.Width = axisXModel.BarWidth;
                    double maxValue = AxisY.Titles.Max(i => i.Value);
                    radioButton.Height = (data.Value / maxValue) * (this.ActualHeight - BottomGrid.Height - HeaderHeight + 10);
                    var linearBrush = new LinearGradientBrush()
                    {
                        StartPoint = new Point(1, 0),
                        EndPoint = new Point(1, 1),
                        GradientStops = new GradientStopCollection() { 
                                            new GradientStop()
                                            {
                                                Color = data.FillBrush, Offset = 0
                                            }, new GradientStop()
                                            {
                                                Color = data.FillEndBrush, Offset = 1
                                            }
                                        }       
                    };
                    radioButton.Background = linearBrush;
                    radioButton.HorizontalAlignment = HorizontalAlignment.Center;
                    radioButton.Click += RadioButton_OnClick;
                    stackPanel.Children.Add(radioButton);

                    //底部
                    var bottomTbl = new TextBlock();
                    bottomTbl.Text = data.Name;
                    bottomTbl.Foreground = axisXModel.ForeGround;
                    bottomTbl.VerticalAlignment = VerticalAlignment.Center;
                    bottomTbl.TextAlignment = TextAlignment.Center;
                    bottomTbl.HorizontalAlignment = HorizontalAlignment.Center;
                    double textBlockWidth = axisXModel.LabelWidth;
                    bottomTbl.Width = axisXModel.LabelWidth;

                    stackPanel.Children.Add(bottomTbl);
                    Grid.SetColumn(stackPanel, index);
                    stackPanel.Margin = new Thickness(0, 0, -textBlockWidth / 2, -17);
                    stackPanel.VerticalAlignment = VerticalAlignment.Bottom;
                    stackPanel.HorizontalAlignment = HorizontalAlignment.Right;

                    MainBarContent.Children.Add(stackPanel);
                    MainBarContent.Background = Brushes.Transparent;
                    index++;
                }
                double mainBarContentWidth = count * barArea;
                if (mainBarContentWidth > MainGridFrom0To1Content.ActualWidth)
                {
                    BtnLeft.Visibility = Visibility.Visible;
                    BtnRight.Visibility = Visibility.Visible;
                }
            }

        }

        public delegate void BarSelectionChangedEventArgs(object sender, RoutedEventArgs e);

        public event BarSelectionChangedEventArgs BarSelectionChanged;
        private void RadioButton_OnClick(object sender, RoutedEventArgs e)
        {
            var currentButton = sender as RadioButton;
            foreach (var element in MainBarContent.Children)
            {
                var stackPanel = element as StackPanel;
                var button = stackPanel.Children[1] as RadioButton;
                if (button != currentButton)
                {
                    button.IsChecked = false;
                }
            }
            if (BarSelectionChanged!=null)
            {
                BarSelectionChanged(sender, e);
            }
        }
        private void SetYTitlesContent()
        {
            var axisYModel = AxisY;
            	   

您的分享是我們最大的動力!

-Advertisement-
Play Games
更多相關文章
  • 經過近一步完善調整,現將本系統源碼正式開放,定名為:EasyuiAdminFramework,另外EasyuiAdminTemplate及EasyuiFlatTheme也一併開源 項目主頁:http://git.oschina.net/gudufy/easyui-admin-framework 技術 ...
  • 最近研究程式的優化,壓縮html可以減小html的體積,有利於提高頁面的相應速度。在webform的basepage中添加如下代碼: 具體到相應項目當中,還是需要調試以下,以免過濾需要輸出的內容。 ...
  • 對於跨平臺的.netCore來說,讓它的程式運行在Linux系統上已經成為必然,也是一種趨勢,畢竟我們的很多服務都放在linux伺服器上(redis,mongodb,myql,fastDFS,lucene),而我們希望與這些組件服務通訊,需要使用的代碼為java,python等,而這些都不是我們擅長 ...
  • 1、 將基礎類型轉為byte數組存儲 2.C#中結構體 與 位元組流 相互轉化 3. C# 結構體位元組對齊 在上述結構體與位元組流轉換第二種方法中,獲取結構體長度int size = Marshal.SizeOf(Mystruct);,並不是13,而是16。在記憶體特定類型數據結構起始地址通常有一定的對齊 ...
  • 1、toastr http://www.jq22.com/jquery-info476 2、jquery1.11.1 checkbox前端js代碼: 單獨使用attr方法checked屬性不改變,單獨使用prop方法屬性改變,頁面checkbox不打勾,兩者配合就沒問題,可能是版本問題 3、jque ...
  • 開始接觸 LINQ 序 在此之前曾發表過三篇關於 LINQ 的隨筆: 進階:《LINQ 標準查詢操作概述》(強烈推薦) 技巧:《Linq To Objects - 如何操作字元串》 和 《Linq To Objects - 如何操作文件目錄》 現在,自己打算再整理一篇關於 LINQ 入門的隨筆,也是 ...
  • DataGridView:顯示數據表,通過此控制項中可以實現連接資料庫,實現數據的增刪改查 一、後臺數據綁定: List<xxx> list = new List<xxx>(); dataGridView1.DataSource = list; //設置不自動生成列,此屬性在屬性面板中沒有 dataG ...
  • 介紹創建F#項目,F#中的模塊以及與C#項目互相引用需要註意的問題。 ...
一周排行
    -Advertisement-
    Play Games
  • 移動開發(一):使用.NET MAUI開發第一個安卓APP 對於工作多年的C#程式員來說,近來想嘗試開發一款安卓APP,考慮了很久最終選擇使用.NET MAUI這個微軟官方的框架來嘗試體驗開發安卓APP,畢竟是使用Visual Studio開發工具,使用起來也比較的順手,結合微軟官方的教程進行了安卓 ...
  • 前言 QuestPDF 是一個開源 .NET 庫,用於生成 PDF 文檔。使用了C# Fluent API方式可簡化開發、減少錯誤並提高工作效率。利用它可以輕鬆生成 PDF 報告、發票、導出文件等。 項目介紹 QuestPDF 是一個革命性的開源 .NET 庫,它徹底改變了我們生成 PDF 文檔的方 ...
  • 項目地址 項目後端地址: https://github.com/ZyPLJ/ZYTteeHole 項目前端頁面地址: ZyPLJ/TreeHoleVue (github.com) https://github.com/ZyPLJ/TreeHoleVue 目前項目測試訪問地址: http://tree ...
  • 話不多說,直接開乾 一.下載 1.官方鏈接下載: https://www.microsoft.com/zh-cn/sql-server/sql-server-downloads 2.在下載目錄中找到下麵這個小的安裝包 SQL2022-SSEI-Dev.exe,運行開始下載SQL server; 二. ...
  • 前言 隨著物聯網(IoT)技術的迅猛發展,MQTT(消息隊列遙測傳輸)協議憑藉其輕量級和高效性,已成為眾多物聯網應用的首選通信標準。 MQTTnet 作為一個高性能的 .NET 開源庫,為 .NET 平臺上的 MQTT 客戶端與伺服器開發提供了強大的支持。 本文將全面介紹 MQTTnet 的核心功能 ...
  • Serilog支持多種接收器用於日誌存儲,增強器用於添加屬性,LogContext管理動態屬性,支持多種輸出格式包括純文本、JSON及ExpressionTemplate。還提供了自定義格式化選項,適用於不同需求。 ...
  • 目錄簡介獲取 HTML 文檔解析 HTML 文檔測試參考文章 簡介 動態內容網站使用 JavaScript 腳本動態檢索和渲染數據,爬取信息時需要模擬瀏覽器行為,否則獲取到的源碼基本是空的。 本文使用的爬取步驟如下: 使用 Selenium 獲取渲染後的 HTML 文檔 使用 HtmlAgility ...
  • 1.前言 什麼是熱更新 游戲或者軟體更新時,無需重新下載客戶端進行安裝,而是在應用程式啟動的情況下,在內部進行資源或者代碼更新 Unity目前常用熱更新解決方案 HybridCLR,Xlua,ILRuntime等 Unity目前常用資源管理解決方案 AssetBundles,Addressable, ...
  • 本文章主要是在C# ASP.NET Core Web API框架實現向手機發送驗證碼簡訊功能。這裡我選擇是一個互億無線簡訊驗證碼平臺,其實像阿裡雲,騰訊雲上面也可以。 首先我們先去 互億無線 https://www.ihuyi.com/api/sms.html 去註冊一個賬號 註冊完成賬號後,它會送 ...
  • 通過以下方式可以高效,並保證數據同步的可靠性 1.API設計 使用RESTful設計,確保API端點明確,並使用適當的HTTP方法(如POST用於創建,PUT用於更新)。 設計清晰的請求和響應模型,以確保客戶端能夠理解預期格式。 2.數據驗證 在伺服器端進行嚴格的數據驗證,確保接收到的數據符合預期格 ...