ChartControl.cs
Description du code
ChartControl.cs est un fichier du projet BrolDev.Ce fichier est situé dans /var/www/bin/sniplets/bibliobrol/broldev/src/.
Projet BrolDev : Librairie de composants réutilisables pour les applications BrolDev en CSharp.
Code source ou contenu du fichier
Code c# (ChartControl.cs) (431 lignes)
using System; using System.Collections; using System.Collections.Generic; using System.ComponentModel; using System.Drawing; using System.Windows.Forms; using be.gaudry.events; using be.gaudry.model; using be.gaudry.model.comparators; using be.gaudry.model.drawing.chart; using be.gaudry.view.utils; namespace be.gaudry.view.controls { public partial class ChartControl : UserControl { #region constructor and declarations private List<Color> colors; private int alpha, minPercent, topCount; private float inactivePosition, selectedPosition; private PieChartControl chartCtl; private bool useLegend, useBgColorForValues; public ChartControl() { alpha = 100; minPercent = 2; inactivePosition = 0F; selectedPosition = 1F; topCount = 0; useLegend = true; InitializeComponent(); this.SetStyle(ControlStyles.ResizeRedraw, false); this.SetStyle(ControlStyles.OptimizedDoubleBuffer, true); this.SetStyle(ControlStyles.AllPaintingInWmPaint, true); /*this.SetStyle(ControlStyles.UserPaint, true); this.SetStyle(ControlStyles.DoubleBuffer, true); this.SetStyle(ControlStyles.ResizeRedraw, true);*/ } #endregion #region properties [ Category("Display"), Browsable(true), Description("Event called to format a double value to get string to display on value column") ] public event FormatStringFromDoubleEventHandler FormatValue; [ Category("Display"), Browsable(true), Description("Event called to format a string to display on the pie chart parts") ] public event FormatStringEventHandler FormatString; /// <summary> /// Title of the group box /// </summary> [ Category("Chart options"), Browsable(true), Description("Title displayed on the GroupBox") ] public String Title { get { return this.mainGB.Text; } set { this.mainGB.Text = value; } } /// <summary> /// Minimum percentage required to display text on pie chart /// </summary> [ Category("Chart options"), Browsable(true), Description("Minimum percentage required to display text on pie chart") ] public int MinPercentToDisplay { get { return this.minPercent; } set { this.minPercent = value; } } /// <summary> /// Count of most significative items to display on the pie chart /// </summary> [ Category("Chart options"), Browsable(true), Description("Count of most significative items to display on the pie chart") ] public int TopCountToDisplay { get { return this.topCount; } set { this.topCount = value; } } /// <summary> /// Alpha value of the colors /// </summary> /*public int AlphaColor { get { return this.alpha; } set { this.alpha = (value > 255) ? 255 : (value < 0) ? 0 : value; } }*/ /// <summary> /// Display a legend for each value in the pie chart on a scrollable legend panel /// </summary> [ Category("Chart options"), Browsable(true), Description("Display or hide the scrollable legend panel") ] public bool DisplayLegend { get { return this.useLegend; } set { this.useLegend = value; } } /// <summary> /// Display or hide the toolStrip containing the options buttons /// </summary> [ Category("Chart options"), Browsable(true), Description("Display or hide the toolStrip containing the options buttons"), DefaultValue(true) ] public bool DisplayToolStrip { get { return this.toolStrip.Visible; } set { this.toolStrip.Visible = value; } } /// <summary> /// Display background color for values /// </summary> [ Category("Chart options"), Browsable(true), Description("Display background color for values") ] public bool DisplayBgColorForValues { get { return this.useBgColorForValues; } set { this.useBgColorForValues = value; } } #endregion #region events private void ChartControl_Load(object sender, EventArgs e) { //create a chart object and fill it with default values initializeChart(); } private void chartVSB_Scroll(object sender, ScrollEventArgs e) { try { chartCtl.SliceRelativeHeight = (float)chartVSB.Value / 100 * -1; } catch (Exception) { } } private void chartHSB_Scroll(object sender, ScrollEventArgs e) { try { chartCtl.InitialAngle = (float)chartHSB.Value * -1; } catch (Exception) { } } private void legendDGV_SelectionChanged(object sender, EventArgs e) { foreach (DataGridViewRow row in legendDGV.Rows) { displacements[(int)row.Cells["id"].Value] = (row.Selected) ? selectedPosition : inactivePosition; } chartCtl.SliceRelativeDisplacements = displacements; } #endregion #region private methods /// <summary> /// create a chart object and fill it with default values /// </summary> private void initializeChart() { if (chartCtl == null) { this.SuspendLayout(); //chartCtl.DisplaceOnMouseOn = true; this.chartPanel.Controls.Add(chartCtl); //chartCtl.Name = "chartCtl"; chartCtl.Dock = DockStyle.Fill; chartCtl.ToolTips = null; chartCtl.LeftMargin = 10F; chartCtl.RightMargin = 10F; chartCtl.TopMargin = 10F; chartCtl.BottomMargin = 10F; chartCtl.FitChart = true; chartCtl.SliceRelativeHeight = 0.10F; chartCtl.InitialAngle = 0F; chartCtl.EdgeLineWidth = 1F; chartCtl.EdgeColorType = EdgeColorType.DarkerDarkerThanSurface; this.ResumeLayout(false); } } private Color getColor(int i) { if (colors.Count < 1) { colors.Add(Color.FromArgb(alpha, 175,175,255)); colors.Add(Color.FromArgb(alpha, Color.Magenta)); colors.Add(Color.FromArgb(alpha, 255,75,75)); colors.Add(Color.FromArgb(alpha, Color.Orange)); colors.Add(Color.FromArgb(alpha, Color.Yellow)); colors.Add(Color.FromArgb(alpha, 200, 255, 75)); colors.Add(Color.FromArgb(alpha, 40,200,10)); } //if we are out of bounds, // the first color after the last of the array is the first (circular) if(i >= colors.Count) i = i % colors.Count; return colors[i]; } #endregion #region public methods /// <summary> /// Open a custom print dialog to print the results /// </summary> public void print() { //be.gaudry.view.utils.PrintDGV.Print_DataGridView(legendDGV, mainGB.Text); DgvFactory.print(legendDGV, mainGB.Text, this.ParentForm); } /// <summary> /// Set colors used for the parts of chart /// </summary> /// <param name="colors">List of colors</param> /// <param name="replaceExisting">(bool) false to add colors at existings, true to replace existing colors by the new list</param> public void setColors(List<Color> colors, bool replaceExisting) { if (replaceExisting) this.colors.Clear(); foreach (Color c in colors) { this.colors.Add(Color.FromArgb(alpha, c)); } } public void setColumnTitle(int columnIndex, string columnTitle) { if (columnIndex >= 0 && columnIndex < legendDGV.ColumnCount) { legendDGV.Columns[columnIndex].HeaderCell.Value = columnTitle; } } public void setBrolChart(Stat stat, String title, String columnTitle) { setBrolChart(stat, title); legendDGV.Columns[1].HeaderCell.Value = columnTitle; } public void setBrolChart(Stat stat, String title) { setBrolChart(stat); Title = title; } public void setBrolChart(Stat stat) { mainGB.Visible = false; if (stat.Total <= 0) { return; } initializeChart(); Color color; legendDGV.Rows.Clear(); #region Top X //If we don't want displaying all results if (topCount > 0) { double others = 0; int topCounter = 0; foreach (KeyValuePair<string, double> kvp in stat.Dictionary) { if (topCounter < topCount) { pairs.Add(kvp); topCounter++; } //We must replace the smallest value by the current else { pairs.Sort(comparer); if (pairs[0].Value > kvp.Value) { others += kvp.Value; } else { others += pairs[0].Value; pairs[0] = kvp; } } } int nbToDisplay = (topCounter >= topCount) ? pairs.Count + 1 : topCounter + 1; if (topCounter >= topCount) { chartCtl.Values[topCounter] = others; chartCtl.Texts[topCounter] = "Autres"; chartCtl.ToolTips[topCounter++] = "Somme des éléments restants"; } int i = 0; foreach (KeyValuePair<string, double> kvp in pairs) { chartCtl.Values[i] = kvp.Value; chartCtl.Texts[i] = kvp.Key; chartCtl.ToolTips[i++] = kvp.Key; } //if (topCounter >= topCount) //{ chartCtl.Values[i] = others; chartCtl.Texts[i] = "Autres"; chartCtl.ToolTips[i] = "Somme des éléments restants"; //} } #endregion else { chartCtl.Values = stat.Values; chartCtl.Texts = stat.Texts; chartCtl.ToolTips = stat.Labels; } for (int i = 0; i < chartCtl.Values.Length; i++) { color = getColor(i); colors[i] = color; displacements[i] = inactivePosition; double tempPercent = (chartCtl.Values[i] / stat.Total) * 100; if (FormatValue != null) { legendDGV.Rows.Add(i, chartCtl.Texts[i], tempPercent, FormatValue(chartCtl.Values[i])); } else { legendDGV.Rows.Add(i, chartCtl.Texts[i], tempPercent, chartCtl.Values[i]); } legendDGV.Rows[legendDGV.RowCount - 1].Cells[1].Style.BackColor = Color.FromArgb(255, color); legendDGV.Rows[legendDGV.RowCount - 1].DefaultCellStyle.SelectionBackColor = Color.FromArgb(color.R, color.G, color.B); legendDGV.Rows[legendDGV.RowCount - 1].DefaultCellStyle.SelectionForeColor = Color.Black; if (tempPercent < minPercent) { chartCtl.Texts[i] = ""; } else if(FormatString!=null) { chartCtl.Texts[i] = FormatString(chartCtl.Texts[i]); } } chartCtl.Colors = colors; chartCtl.SliceRelativeDisplacements = displacements; legendDGV.Sort(percentCol, ListSortDirection.Descending); legendDGV.CurrentCell = null; mainGB.Visible = true; } #endregion #region DGV actions private void printTSB_Click(object sender, EventArgs e) { print(); } private void saveDGVTsB_Click(object sender, EventArgs e) { DgvFactory.save(legendDGV, mainGB.Text, this.ParentForm); } #endregion #region DGV sort private void sortAscTitleTsMi_Click(object sender, EventArgs e) { legendDGV.Sort(valCol, ListSortDirection.Ascending); } private void sortAscNbTsMi_Click(object sender, EventArgs e) { legendDGV.Sort(nbCol, ListSortDirection.Ascending); } private void sortAscPercentTsMi_Click(object sender, EventArgs e) { legendDGV.Sort(percentCol, ListSortDirection.Ascending); } private void sortDescTitleTsMi_Click(object sender, EventArgs e) { legendDGV.Sort(valCol, ListSortDirection.Descending); } private void sortDescNbTsMi_Click(object sender, EventArgs e) { legendDGV.Sort(nbCol, ListSortDirection.Descending); } private void sortDescPercentTsMi_Click(object sender, EventArgs e) { legendDGV.Sort(percentCol, ListSortDirection.Descending); } #endregion } }
Structure et Fichiers du projet
Afficher/masquer...Icône | Nom | Taille | Modification |
Pas de sous-répertoires. | |||
Icône | Nom | Taille | Modification |
| _ | Répertoire parent | 0 octets | 1732239496 22/11/2024 02:38:16 |
Utilisation de l'explorateur de code
- Navigation :
- Un clic sur une icône de répertoire ouvre ce répertoire pour en afficher les fichiers.
- Lorsque le répertoire en cours ne contient pas de sous-répertoires il est possible de remonter vers le répertoire parent.
- La structure de répertoires en treetable (tableau en forme d'arborescence) n'est plus possibledans cette version.
- Un clic sur une icône de fichier ouvre ce fichier pour en afficher le code avec la coloration syntaxique adaptée en fonction du langage principal utilisé dans le fichier.
- Affichage :
- Il est possible de trier les répertoires ou les fichiers selon certains critères (nom, taille, date).
- Actions :
- Les actions possible sur les fichiers dépendent de vos droits d'utilisateur sur le site. Veuillez activer le mode utilisateur pour activer les actions.
Version en cache
22/11/2024 02:38:16 Cette version de la page est en cache (à la date du 22/11/2024 02:38:16) afin d'accélérer le traitement. Vous pouvez activer le mode utilisateur dans le menu en haut pour afficher la dernère version de la page.Document créé le 16/10/2009, dernière modification le 26/10/2018
Source du document imprimé : https://www.gaudry.be/cs-broldev-source-rf-view/controls/ChartControl.cs.html
L'infobrol est un site personnel dont le contenu n'engage que moi. Le texte est mis à disposition sous licence CreativeCommons(BY-NC-SA). Plus d'info sur les conditions d'utilisation et sur l'auteur.