ActionHandler.cs

Top Previous Topic Next Topic  Print this topic

using System;

using System.Net;

using System.Windows;

using System.Windows.Controls;

using System.Windows.Documents;

using System.Windows.Ink;

using System.Windows.Input;

using System.Windows.Media;

using System.Windows.Media.Animation;

using System.Windows.Shapes;

using Ecrion.Silverlight;

 

namespace Ecrion.Silverlight.Sample.Plugin

{

  public class ActionHandler : IActionHandler

   {

      /// <summary>

      /// true if tool tip is open

      /// </summary>

      private Boolean toolTipIsOpen;

 

      /// <summary>

      /// constructor that initialize toolTipIsOpen with false

      /// </summary>

      public ActionHandler()

       {

          this.toolTipIsOpen = false;

       }

 

      /// <summary>

      /// do nothing on this event

      /// </summary>

      /// <param name="sender">any visual element</param>

      /// <param name="e">mouse events arguments</param>

      /// <returns>

      /// return false because we don't want to allow any other plugins to add their own events

      /// </returns>

      public Boolean MouseEnter(Object sender, MouseEventArgs e)

       {

          return false;

       }

 

      /// <summary>

      /// remove tooltip from chart if this is open

      /// </summary>

      /// <param name="sender">any visual element</param>

      /// <param name="e">mouse events arguments</param>

      /// <returns></returns>

      public Boolean MouseLeave(Object sender, MouseEventArgs e)

       {

          ObjectData senderData = (ObjectData)((FrameworkElement)sender).Tag;

          switch (senderData.Parent.getObjectType.Name)

           {

              case "LegendChartItemData":

              case "PieChartSliceData":

              case "SpiderChartSerieData":

              case "BarChartElement":

              case "LineChartElement":

              case "AreaChartElement":

                   {

                      if (this.toolTipIsOpen)

                       {

                          this.toolTipIsOpen = false;

                          Panel page = (Panel)(((FrameworkElement)sender).Parent);

                          if (page!=null && page.Parent != null)

                               ((Panel)page.Parent).Children.RemoveAt(((Panel)page.Parent).Children.Count - 1);

                       }

                      break;

                   }

           }

          return false;

       }

 

      /// <summary>

      /// do nothing on this event

      /// </summary>

      /// <param name="sender">any visual element</param>

      /// <param name="e">mouse events arguments</param>

      /// <returns>

      /// return false because we don't want to allow any other plugins to add their own events

      /// </returns>

      public Boolean MouseMove(Object sender, MouseEventArgs e)

       {

          return false;

       }

 

      /// <summary>

      /// open a tool tip if visual element is part of a supported chart

      /// </summary>

      /// <param name="sender">any visual element</param>

      /// <param name="e">mouse events arguments</param>

      /// <returns>

      /// return false because we don't want to allow any other plugins to add their own events

      /// </returns>

      public Boolean MouseLeftButtonDown(Object sender, MouseButtonEventArgs e)

       {

          ObjectData senderData = (ObjectData)((FrameworkElement)sender).Tag;

          if (senderData.getObjectType == typeof(PageData))

              return false;

          if (toolTipIsOpen == true)

              return this.MouseLeave(sender, null);

 

          this.toolTipIsOpen = true;

          switch (senderData.Parent.getObjectType.Name)

           {

              case "LegendChartItemData":

                   {

                      LegendChartItemData legendChartItem = (LegendChartItemData)senderData.Parent;

 

                      Panel page = (Panel)(((FrameworkElement)sender).Parent);

 

                       ((Panel)page.Parent).Children.Add(this.CreateToolTipForPieChart(legendChartItem.Slice, e.GetPosition(page)));

 

                      break;

                   }

              case "PieChartSliceData":

                   {

                      PieChartSliceData pieChartSlice = (PieChartSliceData)senderData.Parent;

 

                      Panel page = (Panel)(((FrameworkElement)sender).Parent);

 

                       ((Panel)page.Parent).Children.Add(this.CreateToolTipForPieChart(pieChartSlice.Legend, e.GetPosition(page)));

 

                      break;

                   }

              case "SpiderChartSerieData":

                   {

                      SpiderChartSerieData spiderChartSerieData = (SpiderChartSerieData)senderData.Parent;

 

                      Panel page = (Panel)(((FrameworkElement)sender).Parent);

 

                       ((Panel)page.Parent).Children.Add(this.CreateToolTipForSpiderChart(spiderChartSerieData, e.GetPosition(page)));

 

                      break;

                   }

              case "BarChartElement":

                   {

                      BarChartElement barChartElement = (BarChartElement)senderData.Parent;

 

                      Panel page = (Panel)(((FrameworkElement)sender).Parent);

 

                       ((Panel)page.Parent).Children.Add(this.CreateToolTipForBarChart(barChartElement, e.GetPosition(page)));

 

                      break;

                   }

              case "AreaChartElement":

                   {

                      AreaChartElement areaChartElement = (AreaChartElement)senderData.Parent;

 

                      Panel page = (Panel)(((FrameworkElement)sender).Parent);

 

                       ((Panel)page.Parent).Children.Add(this.CreateToolTipForAreaChart(areaChartElement, e.GetPosition(page)));

 

                      break;

                   }

              case "LineChartElement":

                   {

                      LineChartElement lineChartElement = (LineChartElement)senderData.Parent;

 

                      Panel page = (Panel)(((FrameworkElement)sender).Parent);

 

                       ((Panel)page.Parent).Children.Add(this.CreateToolTipForLineChart(lineChartElement, e.GetPosition(page)));

 

                      break;

                   }

              default:

                   {

                      this.toolTipIsOpen = false;

                      break;

                   }

           }

          return false;

       }

 

      /// <summary>

      /// do nothing on this event

      /// </summary>

      /// <param name="sender">any visual element</param>

      /// <param name="e">mouse events arguments</param>

      /// <returns>

      /// return false because we don't want to allow any other plugins to add their own events

      /// </returns>

      public Boolean MouseLeftButtonUp(Object sender, MouseButtonEventArgs e)

       {

         

          return false;

       }

 

      // return true if this plugin need mouse events for rectangle

      public Boolean RegisterEventsForRectangle()

       {

          return true;

       }

 

     

      /// <summary>

      /// Specifies if mouse events are registered for a rectangle element

      /// </summary>

      /// <returns>return true if this plugin need mouse events for ellipse</returns>

      public Boolean RegisterEventsForEllipse()

       {

          return true;

       }

 

      /// <summary>

      /// Specifies if mouse events are registered for a rectangle element

      /// </summary>

      /// <returns>return true if this plugin need mouse events for line</returns>

      public Boolean RegisterEventsForLine()

       {

          return false;

       }

 

      /// <summary>

      /// Specifies if mouse events are registered for a rectangle element

      /// </summary>

      /// <returns>return true if this plugin need mouse events for text</returns>

      public Boolean RegisterEventsForText()

       {

          return false;

       }

 

      /// <summary>

      /// Specifies if mouse events are registered for a rectangle element

      /// </summary>

      /// <returns>return true if this plugin need mouse events for path</returns>

      public Boolean RegisterEventsForPath()

       {

          return true;

       }

 

      /// <summary>

      /// Specifies if mouse events are registered for a rectangle element

      /// </summary>

      /// <returns>return true if this plugin need mouse events for image</returns>

      public Boolean RegisterEventsForImage()

       {

          return false;

       }

 

      /// <summary>

      /// create a custom tool tip and display to the page on a position give by mousePosition point

      /// </summary>

      /// <param name="legendChartItemData">chart data</param>

      /// <param name="mousePosition">position where we add tool tip</param>

      /// <returns>

      /// a visual element which represent the tooltip

      /// </returns>

      protected internal UIElement CreateToolTipForPieChart(ObjectData legendChartItemData, Point mousePosition)

       {

          TextBlock toolTipContent = new TextBlock();

          Border border = new Border();

 

          foreach (ObjectData child in legendChartItemData.GetChildren())

           {

              if (child.getObjectType.Name == "TextData")

               {

                  TextData textData = (TextData)child;

 

                   toolTipContent.Text = textData.Text;

                   toolTipContent.FontSize = textData.Size;

                   toolTipContent.VerticalAlignment = VerticalAlignment.Center;

                   toolTipContent.HorizontalAlignment = HorizontalAlignment.Center;

               }

              if (child.getObjectType.Name == "PathData")

               {

                   border.Background = Util.BrushClone(((PathData)child).Fill);

               }

              if (child.getObjectType.Name == "RectangleData")

               {

                   border.Background = Util.BrushClone(((RectangleData)child).Fill);

               }

              if (child.getObjectType.Name == "EllipseData")

               {

                   border.Background = Util.BrushClone(((EllipseData)child).Fill);

               }

              if (child.getObjectType.Name == "LineData")

               {

                   border.Background = Util.BrushClone(((LineData)child).Fill);

               }

           }

           border.Child = toolTipContent;

 

           border.BorderBrush = Util.BrushClone(border.Background);

           border.BorderBrush.Opacity = 0.7;

           border.BorderThickness = new Thickness(2);

 

           border.Background = new SolidColorBrush(Colors.White);

           border.Background.Opacity = 0.75;

 

           border.Height = toolTipContent.ActualHeight + 10;

           border.Width = toolTipContent.ActualWidth + 20;

 

           border.CornerRadius = new CornerRadius(5);

 

           border.VerticalAlignment = VerticalAlignment.Top;

           border.HorizontalAlignment = HorizontalAlignment.Left;

          MatrixTransform matrixTranform = new MatrixTransform();

           matrixTranform.Matrix = new Matrix(1, 0, 0, 1, mousePosition.X + 10, mousePosition.Y + 10);

           border.RenderTransform = matrixTranform;

 

          return border;

       }

 

      /// <summary>

      /// create a custom tool tip and display to the page on a position give by mousePosition point

      /// </summary>

      /// <param name="legendChartItemData">chart data</param>

      /// <param name="mousePosition">position where we add tool tip</param>

      /// <returns>

      /// a visual element which represent the tooltip

      /// </returns>

      protected internal UIElement CreateToolTipForSpiderChart(SpiderChartSerieData spiderChartSerieData, Point mousePosition)

       {

          Border border = new Border();

          StackPanel textStack = new StackPanel();

           textStack.Orientation = Orientation.Vertical;

 

          SpiderChartData spiderChartData = (SpiderChartData)(spiderChartSerieData.Parent);

 

          foreach (SpiderChartPointData spiderChartPointData in spiderChartSerieData.PointData)

           {

              SpiderChartAxisData spiderChartAxisData = spiderChartData.GetSpiderChartAxisData(spiderChartPointData.AxisId);

 

              if (spiderChartAxisData != null)

               {

 

 

                  List<ObjectData> textDataList = spiderChartAxisData.GetChildrenByType(typeof(TextData));

 

                  foreach (ObjectData textChild in textDataList)

                   {

                      TextData textData = (TextData)textChild;

                      TextBlock textBlock = new TextBlock();

 

                       textBlock.Text = textData.Text;

                       textBlock.FontSize = textData.Size;

                       textBlock.VerticalAlignment = VerticalAlignment.Center;

                       textBlock.HorizontalAlignment = HorizontalAlignment.Center;

 

                       textBlock.Text += (" : " + Convert.ToString(spiderChartPointData.Value));

 

                       textStack.Children.Add(textBlock);

                   }

               }

           }

          foreach (ObjectData child in spiderChartSerieData.GetChildren())

           {

              if (child.getObjectType.Name == "PathData")

               {

                   border.BorderBrush = Util.BrushClone(((PathData)child).Fill);

               }

              if (child.getObjectType.Name == "RectangleData")

               {

                   border.BorderBrush = Util.BrushClone(((RectangleData)child).Fill);

               }

              if (child.getObjectType.Name == "EllipseData")

               {

                   border.BorderBrush = Util.BrushClone(((EllipseData)child).Fill);

               }

              if (child.getObjectType.Name == "LineData")

               {

                   border.BorderBrush = Util.BrushClone(((LineData)child).Fill);

               }

           }

 

           textStack.Margin = new Thickness(3);

           border.Child = textStack;

 

           border.BorderBrush.Opacity = 0.7;

           border.BorderThickness = new Thickness(3);

 

           border.Background = new SolidColorBrush(Colors.White);

           border.Background.Opacity = 0.75;

 

          //border.Height = border.Height + 10;

          //border.Width =  border.Width + 30;

 

           border.CornerRadius = new CornerRadius(5);

 

           border.VerticalAlignment = VerticalAlignment.Top;

           border.HorizontalAlignment = HorizontalAlignment.Left;

          MatrixTransform matrixTranform = new MatrixTransform();

           matrixTranform.Matrix = new Matrix(1, 0, 0, 1, mousePosition.X + 10, mousePosition.Y + 10);

           border.RenderTransform = matrixTranform;

 

          return border;

       }

 

      /// <summary>

      /// create a custom tool tip and display to the page on a position give by mousePosition point

      /// </summary>

      /// <param name="legendChartItemData">chart data</param>

      /// <param name="mousePosition">position where we add tool tip</param>

      /// <returns>

      /// a visual element which represent the tooltip

      /// </returns>

      protected internal UIElement CreateToolTipForBarChart(BarChartElement barChartElement, Point mousePosition)

       {

          Border border = new Border();

          StackPanel textStack = new StackPanel();

           textStack.Orientation = Orientation.Vertical;

 

          TextBlock category = new TextBlock();

          TextBlock value = new TextBlock();

 

           category.Text = "Category: " + barChartElement.BarChartDataPoint.Category;

           value.Text = "Value: " + barChartElement.BarChartDataPoint.Value;

           category.FontSize = value.FontSize = 10;

           category.VerticalAlignment = value.VerticalAlignment = VerticalAlignment.Center;

           category.HorizontalAlignment = value.HorizontalAlignment = HorizontalAlignment.Center;

 

           textStack.Children.Add(category);

           textStack.Children.Add(value);

 

          foreach (ObjectData child in barChartElement.GetChildren())

           {

              if (child.getObjectType.Name == "PathData")

               {

                   border.BorderBrush = Util.BrushClone(((PathData)child).Fill);

               }

              if (child.getObjectType.Name == "RectangleData")

               {

                   border.BorderBrush = Util.BrushClone(((RectangleData)child).Fill);

               }

              if (child.getObjectType.Name == "EllipseData")

               {

                   border.BorderBrush = Util.BrushClone(((EllipseData)child).Fill);

               }

              if (child.getObjectType.Name == "LineData")

               {

                   border.BorderBrush = Util.BrushClone(((LineData)child).Fill);

               }

           }

 

           textStack.Margin = new Thickness(3);

           border.Child = textStack;

 

           border.BorderBrush.Opacity = 0.7;

           border.BorderThickness = new Thickness(3);

 

           border.Background = new SolidColorBrush(Colors.White);

           border.Background.Opacity = 0.75;

 

          //border.Height = border.Height + 10;

          //border.Width =  border.Width + 30;

 

           border.CornerRadius = new CornerRadius(5);

 

           border.VerticalAlignment = VerticalAlignment.Top;

           border.HorizontalAlignment = HorizontalAlignment.Left;

          MatrixTransform matrixTranform = new MatrixTransform();

           matrixTranform.Matrix = new Matrix(1, 0, 0, 1, mousePosition.X + 10, mousePosition.Y + 10);

           border.RenderTransform = matrixTranform;

 

          return border;

       }

 

      /// <summary>

      /// create a custom tool tip and display to the page on a position give by mousePosition point

      /// </summary>

      /// <param name="legendChartItemData">chart data</param>

      /// <param name="mousePosition">position where we add tool tip</param>

      /// <returns>

      /// a visual element which represent the tooltip

      /// </returns>

      protected internal UIElement CreateToolTipForAreaChart(AreaChartElement areaChartElement, Point mousePosition)

       {

          Border border = new Border();

          StackPanel textStack = new StackPanel();

           textStack.Orientation = Orientation.Vertical;

 

          TextBlock category = new TextBlock();

          TextBlock value = new TextBlock();

 

          foreach (ChartDataPoint chartDataPoint in areaChartElement.AreaChartDataPoint)

           {

               category.Text += chartDataPoint.Category + " ";

               value.Text += chartDataPoint.Value + " ";

               category.FontSize = value.FontSize = 10;

               category.VerticalAlignment = value.VerticalAlignment = VerticalAlignment.Center;

               category.HorizontalAlignment = value.HorizontalAlignment = HorizontalAlignment.Center;

           }

 

           category.Text = "Categories: " + category.Text;

           value.Text = "Values: " + value.Text;

 

           textStack.Children.Add(category);

           textStack.Children.Add(value);

 

          foreach (ObjectData child in areaChartElement.GetChildren())

           {

              if (child.getObjectType.Name == "PathData")

               {

                   border.BorderBrush = Util.BrushClone(((PathData)child).Fill);

               }

              if (child.getObjectType.Name == "RectangleData")

               {

                   border.BorderBrush = Util.BrushClone(((RectangleData)child).Fill);

               }

              if (child.getObjectType.Name == "EllipseData")

               {

                   border.BorderBrush = Util.BrushClone(((EllipseData)child).Fill);

               }

              if (child.getObjectType.Name == "LineData")

               {

                   border.BorderBrush = Util.BrushClone(((LineData)child).Fill);

               }

           }

 

           textStack.Margin = new Thickness(3);

           border.Child = textStack;

 

           border.BorderBrush.Opacity = 0.7;

           border.BorderThickness = new Thickness(3);

 

           border.Background = new SolidColorBrush(Colors.White);

           border.Background.Opacity = 0.75;

 

          //border.Height = border.Height + 10;

          //border.Width =  border.Width + 30;

 

           border.CornerRadius = new CornerRadius(5);

 

           border.VerticalAlignment = VerticalAlignment.Top;

           border.HorizontalAlignment = HorizontalAlignment.Left;

          MatrixTransform matrixTranform = new MatrixTransform();

           matrixTranform.Matrix = new Matrix(1, 0, 0, 1, mousePosition.X + 10, mousePosition.Y + 10);

           border.RenderTransform = matrixTranform;

 

          return border;

       }

 

      /// <summary>

      /// create a custom tool tip and display to the page on a position give by mousePosition point

      /// </summary>

      /// <param name="legendChartItemData">chart data</param>

      /// <param name="mousePosition">position where we add tool tip</param>

      /// <returns>

      /// a visual element which represent the tooltip

      /// </returns>

      protected internal UIElement CreateToolTipForLineChart(LineChartElement lineChartElement, Point mousePosition)

       {

          Border border = new Border();

          StackPanel textStack = new StackPanel();

           textStack.Orientation = Orientation.Vertical;

 

          foreach (ChartDataPoint chartDataPoint in lineChartElement.GetLineChartDataPoint)

           {

              TextBlock toolTip = new TextBlock();

              TextBlock category = new TextBlock();

              TextBlock value = new TextBlock();

 

               toolTip.FontSize = value.FontSize = 10;

               toolTip.VerticalAlignment = value.VerticalAlignment = VerticalAlignment.Center;

               toolTip.HorizontalAlignment = value.HorizontalAlignment = HorizontalAlignment.Center;

 

               toolTip.Text = "For " + chartDataPoint.Category + " value is " + chartDataPoint.Value;

               category.Text = String.Empty;

               value.Text = String.Empty;

 

               category.Text = category.Text + chartDataPoint.Category + " ";

               value.Text = value.Text + chartDataPoint.Value + " ";

 

               category.Text = "Categories: " + category.Text;

               value.Text = "Values: " + value.Text;

 

              //textStack.Children.Add(category);

              //textStack.Children.Add(value);

 

               textStack.Children.Add(toolTip);

           }

 

 

          foreach (ObjectData child in lineChartElement.GetChildren())

           {

              if (child.getObjectType.Name == "PathData")

               {

                   border.BorderBrush = Util.BrushClone(((PathData)child).Fill);

               }

              if (child.getObjectType.Name == "RectangleData")

               {

                   border.BorderBrush = Util.BrushClone(((RectangleData)child).Fill);

               }

              if (child.getObjectType.Name == "EllipseData")

               {

                   border.BorderBrush = Util.BrushClone(((EllipseData)child).Fill);

               }

              if (child.getObjectType.Name == "LineData")

               {

                   border.BorderBrush = Util.BrushClone(((LineData)child).Fill);

               }

           }

 

           textStack.Margin = new Thickness(3);

           border.Child = textStack;

 

           border.BorderBrush.Opacity = 0.7;

           border.BorderThickness = new Thickness(3);

 

           border.Background = new SolidColorBrush(Colors.White);

           border.Background.Opacity = 0.75;

 

          //border.Height = border.Height + 10;

          //border.Width =  border.Width + 30;

 

           border.CornerRadius = new CornerRadius(5);

 

           border.VerticalAlignment = VerticalAlignment.Top;

           border.HorizontalAlignment = HorizontalAlignment.Left;

          MatrixTransform matrixTranform = new MatrixTransform();

           matrixTranform.Matrix = new Matrix(1, 0, 0, 1, mousePosition.X + 10, mousePosition.Y + 10);

           border.RenderTransform = matrixTranform;

 

          return border;

       }

 

   }

}