//****************************************************************************************************** // ChartExtensions.cs - Gbtc // // Copyright © 2016, Grid Protection Alliance. All Rights Reserved. // // Licensed to the Grid Protection Alliance (GPA) under one or more contributor license agreements. See // the NOTICE file distributed with this work for additional information regarding copyright ownership. // The GPA licenses this file to you under the MIT License (MIT), the "License"; you may // not use this file except in compliance with the License. You may obtain a copy of the License at: // // http://opensource.org/licenses/MIT // // Unless agreed to in writing, the subject software distributed under the License is distributed on an // "AS-IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. Refer to the // License for the specific language governing permissions and limitations. // // Code Modification History: // ---------------------------------------------------------------------------------------------------- // 08/09/2016 - Stephen C. Wills // Generated original version of source code. // //****************************************************************************************************** using System; using System.Drawing; using System.IO; using System.Windows.Forms.DataVisualization.Charting; namespace GSF.Windows.Forms { /// /// Defines extension methods for s. /// public static class ChartExtensions { /// /// Sets the size of the chart while also setting the font size of the /// axes and legend as well as the border width of each of the series. /// /// The chart to be resized. /// The new width of the chart. /// The new height of the chart. /// The ratio of chart height to font size. /// The ratio of chart height to border width. public static void SetChartSize(this Chart chart, int width, int height, double fontRatio = 37.0D, double borderRatio = 480.0D) { int fontSize = (int)Math.Round(height / fontRatio); int borderWidth = (int)Math.Round(height / borderRatio); chart.Width = width; chart.Height = height; chart.ChartAreas[0].AxisX.LabelAutoFitMaxFontSize = fontSize; chart.ChartAreas[0].AxisY.LabelAutoFitMaxFontSize = fontSize; chart.ChartAreas[0].AxisX.LabelAutoFitMinFontSize = fontSize; chart.ChartAreas[0].AxisY.LabelAutoFitMinFontSize = fontSize; chart.ChartAreas[0].AxisX.TitleFont = new Font(chart.ChartAreas[0].AxisX.TitleFont.FontFamily, fontSize); chart.ChartAreas[0].AxisY.TitleFont = new Font(chart.ChartAreas[0].AxisY.TitleFont.FontFamily, fontSize); chart.Legends[0].Font = new Font(chart.Legends[0].Font.FontFamily, fontSize, FontStyle.Regular); foreach (Series series in chart.Series) series.BorderWidth = borderWidth; } /// /// Saves the chart to a memory stream using the given image /// and returns the stream, ready for reading. /// /// The chart to be saved to the stream. /// The format to used to save the image. /// The stream containing the chart as an image. public static Stream ConvertToImageStream(this Chart chart, ChartImageFormat format = ChartImageFormat.Png) { MemoryStream stream = new MemoryStream(); chart.SaveImage(stream, format); stream.Position = 0; return stream; } } }