WPF 實現柱形統計圖

?WPF 實現柱形統計圖

WPF 實現柱形統計圖

作者:WPFDevelopersOrg

原文鏈接:? ?https://github.com/WPFDevelopersOrg/WPFDevelopers

  • 框架使用大于等于.NET40

  • Visual Studio 2022;

  • 項目使用 MIT 開源許可協議;

  • 避免畫線發虛DrawingContext繪制Line的時候會發虛,以下方法可以避免;

var?d?=?Pen.Thickness?/?2;var?guidelines?=?new?GuidelineSet(new[]?{?d?},?new[]?{?d?});drawingContext.PushGuidelineSet(guidelines);或者調用SnapDrawingExtensions.DrawSnappedLinesBetweenPoints

1) BasicBarChart.cs 代碼如下;

using?System;
using?System.Collections.Generic;
using?System.Linq;
using?System.Windows;
using?System.Windows.Controls;
using?System.Windows.Media;namespace?WPFDevelopers.Controls
{public?class?BasicBarChart?:?Control{public?static?readonly?DependencyProperty?SeriesArrayProperty?=DependencyProperty.Register("SeriesArray",?typeof(IEnumerable<KeyValuePair<string,?double>>),typeof(BasicBarChart),?new?UIPropertyMetadata(SeriesArrayChanged));static?BasicBarChart(){DefaultStyleKeyProperty.OverrideMetadata(typeof(BasicBarChart),new?FrameworkPropertyMetadata(typeof(BasicBarChart)));}public?IEnumerable<KeyValuePair<string,?double>>?SeriesArray{get?=>?(IEnumerable<KeyValuePair<string,?double>>)GetValue(SeriesArrayProperty);set?=>?SetValue(SeriesArrayProperty,?value);}private?static?void?SeriesArrayChanged(DependencyObject?d,?DependencyPropertyChangedEventArgs?e){var?radarChart?=?d?as?BasicBarChart;if?(e.NewValue?!=?null)radarChart.InvalidateVisual();}protected?override?void?OnRender(DrawingContext?drawingContext){//base.OnRender(drawingContext);if?(SeriesArray?==?null?||?SeriesArray.Count()?==?0)return;SnapsToDevicePixels?=?true;UseLayoutRounding?=?true;var?brushConverter?=?new?BrushConverter();var?myPen?=?new?Pen{Thickness?=?1,Brush?=?(Brush)brushConverter.ConvertFromString("#6E7079")};myPen.Freeze();//var?d?=?myPen.Thickness?/?2;//var?guidelines?=?new?GuidelineSet(new[]?{?d?},?new[]?{?d?});//drawingContext.PushGuidelineSet(guidelines);var?h?=?ActualHeight?/?2?+?160;var?w?=?ActualWidth?/?2;var?startX?=?w?/?3;var?width?=?SeriesArray.Count()?*?120?+?startX;//drawingContext.DrawLine(myPen,?new?Point(startX,?h),?new?Point(width,?h));var?stratNum?=?0;drawingContext.DrawSnappedLinesBetweenPoints(myPen,?myPen.Thickness,?new?Point(startX,?h),new?Point(width,?h));var?formattedText?=?DrawingContextHelper.GetFormattedText(stratNum.ToString(),(Brush)brushConverter.ConvertFromString("#6E7079"),?FlowDirection.LeftToRight);drawingContext.DrawText(formattedText,new?Point(startX?-?formattedText.Width?*?2?-?10,?h?-?formattedText.Height?/?2));var?x?=?startX;//var?y?=?h?+?d;var?y?=?h?+?myPen.Thickness;var?points?=?new?List<Point>();var?rectBrush?=?new?SolidColorBrush((Color)ColorConverter.ConvertFromString("#5470C6"));for?(var?i?=?0;?i?<?SeriesArray.Count()?+?1;?i++){//drawingContext.DrawLine(myPen,?new?Point(x,?y),?new?Point(x,?y?+?4));points.Add(new?Point(x,?y));points.Add(new?Point(x,?y?+?4));x?=?x?+?120;}drawingContext.DrawSnappedLinesBetweenPoints(myPen,?myPen.Thickness,?points.ToArray());var?xAxisPen?=?new?Pen{Thickness?=?1,Brush?=?(Brush)brushConverter.ConvertFromString("#E0E6F1")};xAxisPen.Freeze();var?xAxis?=?h?-?80;var?max?=?Convert.ToInt32(SeriesArray.Max(kvp?=>?kvp.Value));max?=?(max?/?50?+?(max?%?50?==?0???0?:?1))?*?50?/?50;var?min?=?Convert.ToInt32(SeriesArray.Min(kvp?=>?kvp.Value));points.Clear();for?(var?i?=?0;?i?<?max;?i++){//drawingContext.DrawLine(xAxisPen,?new?Point(startX,?xAxis),?new?Point(width,?xAxis));points.Add(new?Point(startX,?xAxis));points.Add(new?Point(width,?xAxis));stratNum?+=?50;formattedText?=?DrawingContextHelper.GetFormattedText(stratNum.ToString(),(Brush)brushConverter.ConvertFromString("#6E7079"),?FlowDirection.LeftToRight);drawingContext.DrawText(formattedText,new?Point(startX?-?formattedText.Width?-?10,?xAxis?-?formattedText.Height?/?2));xAxis?=?xAxis?-?80;}drawingContext.DrawSnappedLinesBetweenPoints(xAxisPen,?xAxisPen.Thickness,?points.ToArray());x?=?startX;var?rectWidth?=?85;var?rectHeight?=?0D;for?(var?i?=?0;?i?<?SeriesArray.Count();?i++){formattedText?=?DrawingContextHelper.GetFormattedText(SeriesArray.ToList()[i].Key,(Brush)brushConverter.ConvertFromString("#6E7079"),?FlowDirection.LeftToRight);drawingContext.DrawText(formattedText,?new?Point(x?+?120?/?2?-?formattedText.Width?/?2,?y?+?4));var?_value?=?SeriesArray.ToList()[i].Value;//rectHeight?=?_value?*?200;rectHeight?=?(_value?-?0)?/?(stratNum?-?0)?*?(80?*?max);//rectHeight?=?(stratNum?-?_value)?/?100?*?stratNum;drawingContext.DrawRectangle(rectBrush,?null,new?Rect(x?+?(120?-?85)?/?2,?h?-?rectHeight,?rectWidth,?rectHeight));x?=?x?+?120;}}}
}

2) **BasicBarChartExample.xaml ** 代碼如下;

<UserControl?x:Class="WPFDevelopers.Samples.ExampleViews.BasicBarChartExample"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:WPFDevelopers.Samples.ExampleViews"xmlns:wpfdev="https://github.com/WPFDevelopersOrg/WPFDevelopers"mc:Ignorable="d"?d:DesignHeight="450"?d:DesignWidth="800"><Grid?Background="Gainsboro"><BorderHeight="500"Background="White"Margin="30,0"><Grid?Margin="20,10"><Grid.RowDefinitions><RowDefinition?Height="40"?/><RowDefinition?/><RowDefinition?Height="auto"?/></Grid.RowDefinitions><WrapPanel?HorizontalAlignment="Right"><RectangleWidth="6"Height="26"Fill="Black"?/><TextBlockPadding="10,0"FontSize="24"FontWeight="Black"Text="{Binding?KeyBarChart,?RelativeSource={RelativeSource?AncestorType=local:BasicBarChartExample}}"?/></WrapPanel><wpfdev:BasicBarChartGrid.Row="1"SeriesArray="{Binding?SeriesModels,?RelativeSource={RelativeSource?AncestorType=local:BasicBarChartExample}}"/><ButtonGrid.Row="2"Width="200"VerticalAlignment="Bottom"Click="Button_Click"Content="刷新"Style="{StaticResource?PrimaryButton}"?/></Grid></Border></Grid>
</UserControl>

2) BasicBarChartExample .xaml.cs 代碼如下;

using?System;
using?System.Collections.Generic;
using?System.Linq;
using?System.Text;
using?System.Windows;
using?System.Windows.Controls;
using?System.Windows.Data;
using?System.Windows.Documents;
using?System.Windows.Input;
using?System.Windows.Media;
using?System.Windows.Media.Imaging;
using?System.Windows.Navigation;
using?System.Windows.Shapes;namespace?WPFDevelopers.Samples.ExampleViews
{///?<summary>///?BasicBarChartExample.xaml?的交互邏輯///?</summary>public?partial?class?BasicBarChartExample?:?UserControl{public?IEnumerable<KeyValuePair<string,?double>>?SeriesModels{get?{?return?(IEnumerable<KeyValuePair<string,?double>>)GetValue(SeriesModelsProperty);?}set?{?SetValue(SeriesModelsProperty,?value);?}}public?static?readonly?DependencyProperty?SeriesModelsProperty?=DependencyProperty.Register("SeriesModels",?typeof(IEnumerable<KeyValuePair<string,?double>>),?typeof(BasicBarChartExample),?new?PropertyMetadata(null));Dictionary<string,?IEnumerable<KeyValuePair<string,?double>>>?keyValues?=?new?Dictionary<string,?IEnumerable<KeyValuePair<string,?double>>>();public?string?KeyBarChart{get?{?return?(string)GetValue(KeyBarChartProperty);?}set?{?SetValue(KeyBarChartProperty,?value);?}}public?static?readonly?DependencyProperty?KeyBarChartProperty?=DependencyProperty.Register("KeyBarChart",?typeof(string),?typeof(BasicBarChartExample),?new?PropertyMetadata(null));private?int?_index?=?0;public?BasicBarChartExample(){InitializeComponent();var?Models1?=?new[]{new?KeyValuePair<string,?double>("Mon",?120),new?KeyValuePair<string,?double>("Tue",?130),new?KeyValuePair<string,?double>("Wed",?160),new?KeyValuePair<string,?double>("Thu",?140),new?KeyValuePair<string,?double>("Fri",?200)?,new?KeyValuePair<string,?double>("Sat",?80)?,new?KeyValuePair<string,?double>("Sun",?90)?,};keyValues.Add("到訪數",?Models1);var?Models2?=?new[]{new?KeyValuePair<string,?double>("蛐蛐",?120),new?KeyValuePair<string,?double>("常威",?170),new?KeyValuePair<string,?double>("來福",?30),new?KeyValuePair<string,?double>("包龍星",?200),new?KeyValuePair<string,?double>("包有為",?100)?,new?KeyValuePair<string,?double>("雷豹",?180)?,new?KeyValuePair<string,?double>("方唐鏡",?90)?,};keyValues.Add("能力值",?Models2);SeriesModels?=?keyValues.ToList()[0].Value;KeyBarChart?=?keyValues.ToList()[0].Key;}private?void?Button_Click(object?sender,?RoutedEventArgs?e){_index++;if?(_index?>=?keyValues.Count){_index?=?0;}SeriesModels?=?keyValues.ToList()[_index].Value;KeyBarChart?=?keyValues.ToList()[_index].Key;}}
}
5e32ef9803210a13b60f15bb722fbaa6.png

本文來自互聯網用戶投稿,該文觀點僅代表作者本人,不代表本站立場。本站僅提供信息存儲空間服務,不擁有所有權,不承擔相關法律責任。
如若轉載,請注明出處:http://www.pswp.cn/news/284032.shtml
繁體地址,請注明出處:http://hk.pswp.cn/news/284032.shtml
英文地址,請注明出處:http://en.pswp.cn/news/284032.shtml

如若內容造成侵權/違法違規/事實不符,請聯系多彩編程網進行投訴反饋email:809451989@qq.com,一經查實,立即刪除!

相關文章

Win11卸載WSL,卸載Windows子系統

雖然 Linux 發行版可以通過 Microsoft Store 安裝&#xff0c;但不能通過 Microsoft Store 卸載。 可以通過下列命令卸載。 1、查看當前環境安裝的wsl wsl --list2、注銷&#xff08;卸載&#xff09;當前安裝的Linux的Windows子系統 wsl --unregister Ubuntu3、卸載成功&#…

100億人口會挨餓嗎?人工智能迎擊全球糧食問題

給作物看病的AI、走路“長眼”的拖拉機、上帝視角的衛星數據分析——未來吃飯就靠它們了。 圖片來源&#xff1a;Blue River Technology 人類又面臨了一項危機——隨著人口不斷膨脹&#xff0c;到2050年人類總人口也許要達到100億&#xff0c;然而&#xff0c;地球卻沒有等比例…

Python學習總結15:時間模塊datetime time calendar (二)

二 、datetime模塊 1. datetime中常量 1&#xff09;datetime.MINYEAR&#xff0c;表示datetime所能表示的最小年份&#xff0c;MINYEAR 1。 2&#xff09;datetime.MAXYEAR&#xff0c;表示datetime所能表示的最大年份&#xff0c;MAXYEAR 9999。 2. datetime中的常見類 da…

switch注意事項

Day03_SHJavaTraining_4-5-2017 switch注意事項&#xff1a;①switch語句接受的數據類型  switch語句中的表達式的數據類型,是有要求的    JDK1.0 - 1.4 數據類型接受 byte short int char    JDK1.5 數據類型接受 byte short int char enum(枚舉)  …

WSL1 和 WSL2對比

從 WSL1 更新到 WSL2的主要原因包括&#xff1a; 提高文件系統性能&#xff0c;支持完全的系統調用兼容性。 WSL 2 使用最新、最強大的虛擬化技術在輕量級實用工具虛擬機 (VM) 中運行 Linux 內核。 但是&#xff0c;WSL 2 不是傳統的 VM 體驗。 ? 本指南將比較 WSL 1 和 WSL …

SkiaSharp 之 WPF 自繪 粒子花園(案例版)

此案例包含了簡單的碰撞檢測&#xff0c;圓形碰撞檢測方法&#xff0c;也可以說是五環彈球的升級版&#xff0c;具體可以根據例子參考。粒子花園這名字是案例的名字&#xff0c;效果更加具有科技感&#xff0c;很是不錯&#xff0c;搞搞做成背景特效也是不錯的選擇。Wpf 和 Ski…

xshell連接ubuntu

1.更新資料列表 sudo apt-get update2.安裝openssh-server sudo apt-get install openssh-server3.查看ssh服務是否啟動 sudo ps -e | grep ssh4.如果沒有啟動&#xff0c;啟動ssh服務 sudo service ssh start5.查看IP地址 sudo ifconfig如果出現xshell無法輸入密碼的情況…

教你從零開始搭建一款前端腳手架工具

本文系原創&#xff0c;轉載請附帶作者信息&#xff1a;Jrain Lau項目地址&#xff1a;https://github.com/jrainlau/s...前言 在實際的開發過程中&#xff0c;從零開始建立項目的結構是一件讓人頭疼的事情&#xff0c;所以各種各樣的腳手架工具應運而生。筆者使用較多的yoeman…

微信小程序 --- 頁面跳轉

第一種&#xff1a;wx.navigateTo({}); 跳轉&#xff1a; 注意&#xff1a;這種跳轉回觸發當前頁面的 onHide 方法&#xff0c;將當前頁面隱藏&#xff0c;然后顯示跳轉頁面。所以可以返回&#xff0c;返回的時候觸發 onShow方法進行顯示&#xff1a; &#xff08;項目的底部導…

Java基礎 深拷貝淺拷貝

Java基礎 深拷貝淺拷貝 非基本數據類型 需要new新空間class Student implements Cloneable{private int id;private String name;private Vector course;public Student(){try{Thread.sleep(1000);System.out.println("Student Constructor called.");}catch (Interr…

不安裝運行時運行 .NET 程序

好久沒寫文章了&#xff0c;有些同學問我公眾號是不是廢了&#xff1f;其實并沒有。其實想寫的東西很多很多&#xff0c;主要是最近公司比較忙&#xff0c;以及一些其他個人原因沒有時間來更新文章。這幾天抽空寫了一點點東西&#xff0c;證明公眾號還活著。長久以來的認知&…

一文弄懂分布式和微服務

簡單的說&#xff0c;微服務是架構設計方式&#xff0c;分布式是系統部署方式&#xff0c;兩者概念不同。 微服務 簡單來說微服務就是很小的服務&#xff0c;小到一個服務只對應一個單一的功能&#xff0c;只做一件事。這個服務可以單獨部署運行&#xff0c;服務之間可以通過R…

常見的js算法面試題收集,es6實現

1、js 統計一個字符串出現頻率最高的字母/數字 let str asdfghjklaqwertyuiopiaia; const strChar str > {let string [...str],maxValue ,obj {},max 0;string.forEach(value > {obj[value] obj[value] undefined ? 1 : obj[value] 1if (obj[value] > max)…

PHP面向對象(OOP)----分頁類

PHP面向對象(OOP)----分頁類 同驗證碼類&#xff0c;分頁也是在個人博客&#xff0c;論壇等網站中不可缺少的方式&#xff0c;通過分頁可以在一個界面展示固定條數的數據&#xff0c;而不至于將所有數據全部羅列到一起&#xff0c;實現分頁的原理其實就是對數據庫查詢輸出加了一…

JS 事件練習

QQ拖拽及狀態欄選擇 HTML 1 <!DOCTYPE html>2 <html xmlns"http://www.w3.org/1999/xhtml">3 <head>4 <title>QQ練習</title>5 <link href"css/main.css" rel"stylesheet" />6 <script src&…

Dubbo和Spring Cloud微服務架構對比

微服務架構是互聯網很熱門的話題&#xff0c;是互聯網技術發展的必然結果。它提倡將單一應用程序劃分成一組小的服務&#xff0c;服務之間互相協調、互相配合&#xff0c;為用戶提供最終價值。目錄 微服務主要的優勢 降低復雜度 可獨立部署 容錯 擴展 核心部件 總體架構 Dubbo …

《ABP Framework 極速開發》 - 教程首發

?寫在發布之前強烈建議每一位小伙伴都應該好好看看 ABP Framework 官方文檔&#xff0c;可能有很多的小伙伴跟我剛開始的感覺一樣“一看文檔深似海”&#xff0c;看完文檔之后&#xff0c;想要上手卻找不著頭緒。本套教程寫作的目的之一是為初學者提供一條相對簡潔的快速上手路…

智能家居系統結構標準化

版權申明&#xff1a;本文為博主窗戶(Colin Cai)原創&#xff0c;歡迎轉帖。如要轉貼&#xff0c;必須注明原文網址http://www.cnblogs.com/Colin-Cai/p/8490423.html作者&#xff1a;窗戶QQ&#xff1a;6679072E-mail&#xff1a;6679072qq.com0 引 言 智能家居是指利用先進的…

洛谷 P3391 文藝平衡樹

題目描述 您需要寫一種數據結構&#xff08;可參考題目標題&#xff09;&#xff0c;來維護一個有序數列&#xff0c;其中需要提供以下操作&#xff1a;翻轉一個區間&#xff0c;例如原有序序列是5 4 3 2 1&#xff0c;翻轉區間是[2,4]的話&#xff0c;結果是5 2 3 4 1 --by洛谷…

JSONObject中optString和getString等的區別

2019獨角獸企業重金招聘Python工程師標準>>> 同事在看到我寫的解析數據代碼后&#xff0c;告訴我optString比getString好用&#xff0c;optString不會拋異常&#xff0c;而getString會拋異常&#xff0c;自己是將信將疑&#xff0c;就說&#xff0c;回去后我查查資料…