/// <summary> /// Handles the event which occurs when the selected item has changed /// </summary> /// <param name="sender"></param> /// <param name="e"></param> void CollectionViewCurrentChanged(object sender, EventArgs e) { CollectionView collectionView = (CollectionView)sender; if (collectionView != null && collectionView.CurrentPosition >= 0 && collectionView.CurrentPosition <= piePieces.Count) { PiePiece piece = piePieces[collectionView.CurrentPosition]; DoubleAnimation a = new DoubleAnimation(); a.To = 10; a.Duration = new Duration(TimeSpan.FromMilliseconds(500)); piece.BeginAnimation(PiePiece.PushOutProperty, a); } }
/// <summary> /// Handles the event which occurs just before a pie piece tooltip opens /// </summary> /// <param name="sender"></param> /// <param name="e"></param> private void PiePieceToolTipOpening(object sender, ToolTipEventArgs e) { PiePiece piece = (PiePiece)sender; int index = (int)piece.Tag; if (piece.ToolTip != null) { ToolTip tip = (ToolTip)piece.ToolTip; Tag tag = listTag.ElementAt(index) as Tag; tip.Content = "Name: " + tag.Name + "\r\nAmount: " + tag.Amount + "\r\nPercent: " + string.Format(null, @"{0:0%}", piece.Percentage);; } }
/// <summary> /// Constructs pie pieces and adds them to the visual tree for this control's canvas /// </summary> private void ConstructPiePieces() { CollectionView myCollectionView = (CollectionView)CollectionViewSource.GetDefaultView(this.DataContext); if (myCollectionView == null) { return; } double halfWidth = this.Width / 2; double innerRadius = halfWidth * HoleSize; // compute the total for the property which is being plotted double total = 0; foreach (object item in myCollectionView) { total += GetPlottedPropertyValue(item); } // add the pie pieces canvas.Children.Clear(); piePieces.Clear(); double accumulativeAngle = 0.0; foreach (Object item in myCollectionView) { bool selectedItem = item == myCollectionView.CurrentItem; double wedgeAngle = GetPlottedPropertyValue(item) * 360 / total; PiePiece piece = new PiePiece() { Radius = halfWidth, InnerRadius = innerRadius, CentreX = halfWidth, CentreY = halfWidth, PushOut = (selectedItem ? 10.0 : 0), WedgeAngle = wedgeAngle, PieceValue = GetPlottedPropertyValue(item), RotationAngle = accumulativeAngle, Fill = ColorSelector != null?ColorSelector.SelectBrush(item, myCollectionView.IndexOf(item)) : Brushes.Black, // record the index of the item which this pie slice represents Tag = myCollectionView.IndexOf(item), ToolTip = new ToolTip() }; piece.ToolTipOpening += new ToolTipEventHandler(PiePieceToolTipOpening); piece.MouseUp += new MouseButtonEventHandler(PiePieceMouseUp); piePieces.Add(piece); canvas.Children.Insert(0, piece); accumulativeAngle += wedgeAngle; } }
public void ShowPie() { List <PieDataItem> myCollectionView = (List <PieDataItem>) this.DataContext; if (myCollectionView == null) { return; } double halfWidth = PieWidth / 2; //double innerRadius = halfWidth * HoleSize; double innerRadius = 50; double total = 0; foreach (PieDataItem item in myCollectionView) { total += item.Value; } LayoutRoot.Children.Clear(); piePieces.Clear(); double accumulativeAngle = 0; foreach (PieDataItem item in myCollectionView) { bool selectedItem = item == CurrentItem; double wedgeAngle = item.Value * 360.0 / total; PiePiece piece = new PiePiece() { Radius = halfWidth, InnerRadius = innerRadius, CentreX = halfWidth, CentreY = halfWidth, //PushOut = (selectedItem ? 0.5 : 0), PushOut = (selectedItem ? 10 : 0), WedgeAngle = wedgeAngle, PieceValue = item.Value, RotationAngle = accumulativeAngle, Fill = item.Brush, Tag = item }; piece.Tapped += piece_Tapped; piePieces.Add(piece); LayoutRoot.Children.Add(piece); accumulativeAngle += wedgeAngle; } NotifyChange(); }
private PiePiece AddPiePiece() { PiePiece newPiece = new PiePiece(); newPiece.MouseDown += LPiepieceOnMouseUp; PiePiece lastPiece = PieList.LastOrDefault(); if (lastPiece != null) { newPiece.StartAngle = lastPiece.StartAngle + lastPiece.WedgeAngle; } PieList.Add(newPiece); Canvas.SetTop(newPiece, Canvas.ActualHeight / 2); Canvas.SetLeft(newPiece, Canvas.ActualWidth / 2); Canvas.Children.Add(newPiece); return(newPiece); }
/// <summary> /// Handles the event which occurs just before a pie piece tooltip opens /// </summary> /// <param name="sender"></param> /// <param name="e"></param> void PiePieceToolTipOpening(object sender, ToolTipEventArgs e) { PiePiece piece = (PiePiece)sender; CollectionView collectionView = (CollectionView)CollectionViewSource.GetDefaultView(this.DataContext); if (collectionView == null) { return; } // select the item which this pie piece represents int index = (int)piece.Tag; if (piece.ToolTip != null) { ToolTip tip = (ToolTip)piece.ToolTip; tip.DataContext = collectionView.GetItemAt(index); } }
private void LPiepieceOnMouseUp(object sender, MouseButtonEventArgs mouseButtonEventArgs) { PiePiece lPiepiece = sender as PiePiece; if (lPiepiece == null) { return; } if (lPiepiece.PushOut > 0) { MakePieInvisible(lPiepiece); SetCenterLabel(""); } else { SetCenterLabel(TextChange(lPiepiece)); MakePieVisible(lPiepiece); } }
/// <summary> /// Handles the MouseUp event from the individual Pie Pieces /// </summary> /// <param name="sender"></param> /// <param name="e"></param> void PiePieceMouseUp(object sender, MouseButtonEventArgs e) { CollectionView collectionView = (CollectionView)CollectionViewSource.GetDefaultView(this.DataContext); //Индекс для определения текущей позиции int collectionIndex = collectionView.CurrentPosition; if (collectionView == null) { return; } PiePiece piece = sender as PiePiece; if (piece == null) { return; } // Выбираем нужный кусок пирога int index = (int)piece.Tag; if (collectionIndex == index) { if (collectionView != null && collectionView.CurrentPosition >= 0 && collectionView.CurrentPosition <= piePieces.Count) { DoubleAnimation a = new DoubleAnimation(); if (piece.PushOut == 10) { a.To = 0; a.Duration = new Duration(TimeSpan.FromMilliseconds(300)); } else { a.To = 10; a.Duration = new Duration(TimeSpan.FromMilliseconds(500)); } piece.BeginAnimation(PiePiece.PushOutProperty, a); } } else { collectionView.MoveCurrentToPosition(index); } }
/// <summary> /// Handles the MouseUp event from the individual Pie Pieces /// </summary> /// <param name="sender"></param> /// <param name="e"></param> void PiePieceMouseUp(object sender, MouseButtonEventArgs e) { CollectionView collectionView = (CollectionView)CollectionViewSource.GetDefaultView(this.DataContext); if (collectionView == null) { return; } PiePiece piece = sender as PiePiece; if (piece == null) { return; } // select the item which this pie piece represents int index = (int)piece.Tag; collectionView.MoveCurrentToPosition(index); }
private void ConstructPiePieces() { double halfWidth = this.Width / 2; double innerRadius = 2;//halfWidth * HoleSize; // add the pie pieces canvas.Children.Clear(); piePieces.Clear(); double accumulativeAngle = 0; for (int item = 0; item < int.Parse(PlottedProperty); item++) { double wedgeAngle = 360 / int.Parse(PlottedProperty); PiePiece piece = new PiePiece() { Radius = halfWidth, InnerRadius = innerRadius, CentreX = halfWidth, CentreY = halfWidth, PushOut = 0, WedgeAngle = wedgeAngle, PieceValue = wedgeAngle, RotationAngle = accumulativeAngle, Fill = ColorSelector != null?ColorSelector.SelectBrush(null, item) : Brushes.Black, // record the index of the item which this pie slice represents Tag = item, ToolTip = new ToolTip() }; piece.ToolTipOpening += new ToolTipEventHandler(PiePieceToolTipOpening); piece.MouseUp += new MouseButtonEventHandler(PiePieceMouseUp); piePieces.Add(piece); canvas.Children.Insert(0, piece); accumulativeAngle += wedgeAngle; } }
private string CreateDescriptionText(PiePiece pClickedPiePiece) { double lValue = Values[PieList.IndexOf(pClickedPiePiece)]; switch (DescriptionText) { case DescriptionTextType.None: return(""); case DescriptionTextType.Percentage: return(String.Format("{0:0.0}%", lValue * 100 / MaxValue)); case DescriptionTextType.Value: return(String.Format("{0}/{1}", (int)lValue, (int)MaxValue)); case DescriptionTextType.Angle: return(pClickedPiePiece.WedgeAngle.ToString()); default: throw new ArgumentOutOfRangeException(); } }
private static void ValueChangedCallback(DependencyObject dependencyObject, DependencyPropertyChangedEventArgs dependencyPropertyChangedEventArgs) { if (!(dependencyObject is PieChart)) { return; } PieChart chart = dependencyObject as PieChart; if (dependencyPropertyChangedEventArgs.NewValue == null) { foreach (PiePiece piece in chart.PieList) { chart.Canvas.Children.Remove(piece); } chart.PieList.Clear(); return; } if (!(dependencyPropertyChangedEventArgs.NewValue is ObservableCollection <double>)) { return; } ObservableCollection <double> newCollection = dependencyPropertyChangedEventArgs.NewValue as ObservableCollection <double>; int newCount = newCollection.Count - chart.PieList.Count; for (int i = 0; i < newCount; i++) { PiePiece lastPiece = chart.PieList.LastOrDefault(); PiePiece newPiece = chart.AddPiePiece(); if (lastPiece != null) { double newStartAngle = lastPiece.StartAngle + lastPiece.WedgeAngle; newPiece.StartAngle = newStartAngle; } } chart.AnimateChart(); }
private void AddNewPiePiece(FingerEventArgs finger) { DispatcherTimer startTimer = new DispatcherTimer(); startTimer.Tick += new EventHandler(startTimer_Elapsed); startTimer.Interval = TimeSpan.FromMilliseconds(this._holdTime); _holdFingerList.Add(finger.FingerID, startTimer); _holdFingerPositionList.Add(finger.FingerID, finger.Position); PiePiece pie = new PiePiece(); pie.Radius = 24; pie.InnerRadius = 12; pie.Fill = Brushes.Beige; pie.Opacity = .8d; Canvas.SetLeft(pie, finger.Position.X); Canvas.SetTop(pie, finger.Position.Y); PopupStage.Children.Add(pie); pie.BeginAnimation(PiePiece.WedgeAngleProperty, new DoubleAnimation(360, new Duration(TimeSpan.FromMilliseconds(this._holdTime)))); startTimer.Tag = pie; startTimer.Start(); }
private void ConstructPiePieces() { string[] str = PlottedProperty.Split(','); if (int.Parse(str[0]) == 0) { SP.Children.Clear(); return; } if (!string.IsNullOrEmpty(str[2])) { solidColorBrush.Color = (Color)ColorConverter.ConvertFromString(str[2]); } SP.Children.Clear(); if (str.Length == 4 || str.Length == 3) { if (str.Length == 4) { solidColorBrush.Color = (Color)ColorConverter.ConvertFromString(str[3].Split(';')[1]); } StackPanel _StackPanel = new StackPanel(); _StackPanel.Width = 24; _StackPanel.VerticalAlignment = System.Windows.VerticalAlignment.Center; _StackPanel.Height = 24; _StackPanel.Background = Resources["MyDrawingBrush"] as DrawingBrush; SP.Children.Add(_StackPanel); return; } double halfWidth = this.Width / 2; double innerRadius = 2;//halfWidth * HoleSize; // add the pie pieces SP.Children.Clear(); piePieces.Clear(); double accumulativeAngle = 0; StringBuilder stringBuilder = new StringBuilder();//是否扫描到,采血管、阴阳补,选择颜色 int PlottedCount = 0; for (int item = 3; item < str.Length; item++) { string strtmp = str[item].ToString().Split(';')[1]; if (stringBuilder.ToString().IndexOf(strtmp) >= 0) { continue; } stringBuilder.Append(strtmp + ","); PlottedCount++; } if (PlottedCount == 1) { solidColorBrush.Color = (Color)ColorConverter.ConvertFromString(stringBuilder.ToString().Substring(0, stringBuilder.ToString().Length - 1)); StackPanel _StackPanel = new StackPanel(); _StackPanel.Width = 24; _StackPanel.VerticalAlignment = System.Windows.VerticalAlignment.Center; _StackPanel.Height = 24; _StackPanel.Background = Resources["MyDrawingBrush"] as DrawingBrush; SP.Children.Add(_StackPanel); return; } for (int item = 0; item < PlottedCount; item++) { double wedgeAngle = 360 / (PlottedCount); PiePiece piece = new PiePiece() { Radius = halfWidth, InnerRadius = innerRadius, CentreX = halfWidth, CentreY = halfWidth, PushOut = 0, WedgeAngle = wedgeAngle, PieceValue = wedgeAngle, RotationAngle = accumulativeAngle, Fill = new SolidColorBrush((Color)ColorConverter.ConvertFromString(stringBuilder.ToString().Split(',')[item])) //Fill =ColorSelector != null ? ColorSelector.SelectBrush(item, item) : Brushes.Black, // record the index of the item which this pie slice represents //Tag = item, // ToolTip = new ToolTip() }; //piece.ToolTipOpening += new ToolTipEventHandler(PiePieceToolTipOpening); //piece.MouseUp += new MouseButtonEventHandler(PiePieceMouseUp); piePieces.Add(piece); SP.Children.Add(piece); accumulativeAngle += wedgeAngle; } }
private void MakePieInvisible(PiePiece pClickedPiePiece) { DoubleAnimation lHide = new DoubleAnimation(0, new Duration(TimeSpan.FromMilliseconds(IsAnimate ? 200 : 0))); pClickedPiePiece.BeginAnimation(PiePiece.PushOutProperty, lHide); }
private void RemovePiePiece(PiePiece pPiePiece) { PieList.Remove(pPiePiece); Canvas.Children.Remove(pPiePiece); }