Zoom in multiple Areas and Axis Y - c#

I have a project with multiple AxisY, with 3 series and and each series has its axisY associated. When I zoom, I have achieved that all the series have zoom but the scale of the axisYof the secondary axisY doesn't change, only that of the main series changes.
Original Chart:
Chart with zoom applied:
I have create the new axisY secondary with the code of microsoft (Project "Windows Forms Samples Environment for Microsoft Chart Control"):
public void CreateYAxis(Chart chart, ChartArea area, Series series, float axisOffset, float labelsSize)
{
// Create new chart area for original series
ChartArea areaSeries = chart.ChartAreas.Add("ChartArea_" + series.Name);
areaSeries.BackColor = Color.Transparent;
areaSeries.BorderColor = Color.Transparent;
areaSeries.Position.FromRectangleF(area.Position.ToRectangleF());
areaSeries.InnerPlotPosition.FromRectangleF(area.InnerPlotPosition.ToRectangleF());
areaSeries.AxisX.MajorGrid.Enabled = false;
areaSeries.AxisX.MajorTickMark.Enabled = false;
areaSeries.AxisX.LabelStyle.Enabled = false;
areaSeries.AxisY.MajorGrid.Enabled = false;
areaSeries.AxisY.MajorTickMark.Enabled = false;
areaSeries.AxisY.LabelStyle.Enabled = false;
areaSeries.AxisY.IsStartedFromZero = area.AxisY.IsStartedFromZero;
series.ChartArea = areaSeries.Name;
// Create new chart area for axis
ChartArea areaAxis = chart.ChartAreas.Add("AxisY_" + series.ChartArea);
areaAxis.BackColor = Color.Transparent;
areaAxis.BorderColor = Color.Transparent;
areaAxis.Position.FromRectangleF(chart.ChartAreas[series.ChartArea].Position.ToRectangleF());
areaAxis.InnerPlotPosition.FromRectangleF(chart.ChartAreas[series.ChartArea].InnerPlotPosition.ToRectangleF());
// Create a copy of specified series
Series seriesCopy = chart.Series.Add(series.Name + "_Copy");
seriesCopy.ChartType = series.ChartType;
foreach(DataPoint point in series.Points)
{
seriesCopy.Points.AddXY(point.XValue, point.YValues[0]);
}
// Hide copied series
seriesCopy.IsVisibleInLegend = false;
seriesCopy.Color = Color.Transparent;
seriesCopy.BorderColor = Color.Transparent;
seriesCopy.ChartArea = areaAxis.Name;
// Disable drid lines & tickmarks
areaAxis.AxisX.LineWidth = 0;
areaAxis.AxisX.MajorGrid.Enabled = false;
areaAxis.AxisX.MajorTickMark.Enabled = false;
areaAxis.AxisX.LabelStyle.Enabled = false;
areaAxis.AxisY.MajorGrid.Enabled = false;
areaAxis.AxisY.IsStartedFromZero = area.AxisY.IsStartedFromZero;
areaAxis.AxisY.LabelStyle.Font = area.AxisY.LabelStyle.Font;
// Adjust area position
areaAxis.Position.X -= axisOffset;
areaAxis.InnerPlotPosition.X += labelsSize;
}
UPDATE--------------------------------:
I have this news:
Now, the 3 axis update his scale but it comes out together, How to separate them?
The code is here:
chart1.ChartAreas[2].AlignmentOrientation = AreaAlignmentOrientations.All;
chart1.ChartAreas[2].AlignmentStyle = AreaAlignmentStyles.All;
chart1.ChartAreas[2].AlignWithChartArea = "ChartArea1";
chart1.ChartAreas[4].AlignmentOrientation = AreaAlignmentOrientations.All;
chart1.ChartAreas[4].AlignmentStyle = AreaAlignmentStyles.All;
chart1.ChartAreas[4].AlignWithChartArea = "ChartArea1";

Related

problem with save as image chart in spire.xls

I Used spire.xls to create a sheet and a chart but my problem is when I save a chart as a PDF it would be so inappropriate just need to justify the chart but don't know how I think it would be the problem with scale
Chart chart = sheet.Charts.Add(ExcelChartType.ScatterSmoothedLineMarkers);
//Set region of chart data
chart.DataRange = sheet.Range["A2:A" + (_state.NumberOfReads + 2).ToString()].AddCombinedRange(sheet.Range["F2:F" + (_state.NumberOfReads + 2).ToString()]);
chart.SeriesDataFromRange = false;
//Set position of the chart
chart.LeftColumn = 22;
chart.TopRow = 6;
chart.RightColumn = 32;
chart.BottomRow = 29;
//Set and format category axis title
chart.PrimaryCategoryAxis.Title = "Applied Pressure DUT.";
chart.PrimaryCategoryAxis.Font.IsBold = true;
chart.PrimaryCategoryAxis.TitleArea.IsBold = true;
//Set and format value axis title
chart.PrimaryValueAxis.Title = "Average of REF.";
chart.PrimaryValueAxis.HasMajorGridLines = false;
chart.PrimaryValueAxis.TitleArea.TextRotationAngle = -90;
chart.PrimaryValueAxis.MinValue = 0;
chart.PrimaryValueAxis.TitleArea.IsBold = true;
chart.Legend.Delete();
//Loop through the data series of the chart
foreach (ChartSerie cs in chart.Series)
{
cs.Format.Options.IsVaryColor = true;
//Show data labels for data points
cs.DataPoints.DefaultDataPoint.DataLabels.HasValue = true;
}
chart.PrimaryValueAxis.HasMajorGridLines = true;
IChartTrendLine trendLine = chart.Series[0].TrendLines.Add(TrendLineType.Linear);
chart.Series[0].TrendLines[0].DisplayEquation = true;
chart.Series[0].TrendLines[0].DisplayRSquared = true;
//chart.HasDataTable = true;
Image[] imgs = workbook.SaveChartAsImage(sheet);
Bitmap img = new Bitmap(imgs[0]);
img.Save(path + filePic, ImageFormat.Png);
and also look at the outputs

How to set default zoom on chart using Windows Forms - C#

I want to set default zoom only on Axis X but when I use this code my chart looks like:
My first value is way ahead.
This is the code:
var ca = chart1.ChartAreas["ChartArea1"];
ca.CursorX.IsUserEnabled = true;
ca.CursorX.IsUserSelectionEnabled = true;
ca.CursorX.AutoScroll = true;
chart1.Series["Temp"].XValueType = ChartValueType.DateTime;
chart1.ChartAreas[0].AxisX.IntervalType = DateTimeIntervalType.Days;
chart1.ChartAreas[0].AxisX.Interval = 1;
chart1.ChartAreas[0].AxisX.IntervalOffset = 1;
chart1.ChartAreas[0].CursorX.Interval = 0;
chart1.ChartAreas[0].AxisX.ScaleView.Zoomable = true;
chart1.ChartAreas["ChartArea1"].AxisX.ScaleView.Zoom(chart1.Series[0].Points.Count - 10, chart1.Series[0].Points.Count);
chart1.Series["Temp"].MarkerSize = 10;
chart1.Series["Temp"].IsValueShownAsLabel = true;
So I want to set default zoom on chart only on AxisX when I use mouse to zoom twice the chart looks good. Like that:

MS chart candlestick How to set tail colors

I am currently developing a candlestick chart with mschart in visual C#.
I have now created two charts and created the charts as follows
Question 1. View the Candlestick Chart at the top. I would like to apply the tail color of each rod as red or blue.
Question 2. View the bar chart at the bottom. I would like to apply Red or Blue color to this chart. I want to apply the same color to the top of the Candlestick chart. How can I do it ?
[source]
DataTable table_ChartData = new DataTable();
table_ChartData.Columns.Add("Id");
table_ChartData.Columns.Add("Open");
table_ChartData.Columns.Add("Close");
table_ChartData.Columns.Add("High");
table_ChartData.Columns.Add("Low");
table_ChartData.Columns.Add("Day");
dataGridView1.DataSource = table_ChartData;
chart1.ChartAreas["ChartArea1"].AxisX.MajorGrid.LineWidth = 1;
chart1.ChartAreas["ChartArea1"].AxisY.MajorGrid.LineWidth = 1;
chart1.ChartAreas["ChartArea1"].AxisY.Maximum = max;
chart1.ChartAreas["ChartArea1"].AxisY.Minimum = min;
chart1.ChartAreas["ChartArea1"].AxisX.LabelAutoFitStyle = LabelAutoFitStyles.WordWrap;
chart1.ChartAreas["ChartArea1"].AxisX.IsLabelAutoFit = true;
chart1.ChartAreas["ChartArea1"].AxisX.LabelStyle.Enabled = true;
chart1.Series["Candle"].XValueMember = "Day";
chart1.Series["Candle"].YValueMembers = "High,Low,Open,Close,Volume";
chart1.Series["Candle"].XValueType = System.Windows.Forms.DataVisualization.Charting.ChartValueType.Date;
chart1.Series["Candle"].CustomProperties = "PriceDownColor=Blue,PriceUpColor=Red";
chart1.Series["Candle"]["OpenCloseStyle"] = "Triangle";
chart1.Series["Candle"]["ShowOpenClose"] = "Both";
chart1.DataSource = table_ChartData;
chart1.DataBind();
////////////////////////////////////////////////////////////
chart2.ChartAreas["ChartArea1"].AxisX.MajorGrid.LineWidth = 1;
chart2.ChartAreas["ChartArea1"].AxisY.MajorGrid.LineWidth = 1;
chart2.ChartAreas["ChartArea1"].AxisY.Maximum = v_max + (v_max / 10);
chart2.ChartAreas["ChartArea1"].AxisY.Minimum = v_min / 2;
chart2.ChartAreas["ChartArea1"].AxisX.LabelAutoFitStyle = LabelAutoFitStyles.WordWrap;
chart2.ChartAreas["ChartArea1"].AxisX.IsLabelAutoFit = true;
chart2.ChartAreas["ChartArea1"].AxisX.LabelStyle.Enabled = true;
chart2.Series["Bar"].XValueMember = "Day";
chart2.Series["Bar"].YValueMembers = "Volume";
chart2.Series["Bar"].XValueType = System.Windows.Forms.DataVisualization.Charting.ChartValueType.Date;
chart2.Series["Bar"].YValueType = System.Windows.Forms.DataVisualization.Charting.ChartValueType.Int32;
chart2.DataSource = table_ChartData;
chart2.DataBind();
In a Candlestick Chart there are CustomProperties to automatiaclly set the colors of the boxes, depending on the trend:
someSeries.SetCustomProperty("PriceUpColor", "Green");
someSeries.SetCustomProperty("PriceDownColor", "Red");
Unfortunately there is no way to set the colors of the lines that connect the high- and low-values.
But, unless you have messed with other Custom attributes and if the x-values are meaningful, you can easily draw those lines, and by drawing the top and the bottom part separately you can also use different colors.
Here is an example:
private void chart6_PostPaint(object sender, ChartPaintEventArgs e)
{
ChartArea ca = chart6.ChartAreas[0];
Series s = chart6.Series[0];
Pen hiPen = Pens.Green;
Pen loPen = Pens.Red;
if (e.ChartElement == s)
foreach (DataPoint dp in s.Points)
{
float x = (float)ca.AxisX.ValueToPixelPosition(dp.XValue);
float y_hi = (float)ca.AxisY.ValueToPixelPosition(dp.YValues[0]);
float y_low = (float)ca.AxisY.ValueToPixelPosition(dp.YValues[1]);
float y_open = (float)ca.AxisY.ValueToPixelPosition(dp.YValues[2]);
float y_close = (float)ca.AxisY.ValueToPixelPosition(dp.YValues[3]);
e.ChartGraphics.Graphics.DrawLine(hiPen, x, y_low, x, Math.Min(y_close, y_open));
e.ChartGraphics.Graphics.DrawLine(loPen, x, y_hi, x, Math.Max(y_close, y_open));
}
}
To set Colors for the 2nd Chart's points you need to loop over the points as there Colors cannot be set in binding.
The code is simple:
void SetColors(Series candles, Series columns)
{
for (int i = 0; i < candles.Points.Count; i++)
{
DataPoint dp = candles.Points[i];
columns.Points[i].Color =
dp.YValues[2] > dp.YValues[3] ? Color.Red : Color.Green;
}
}
Call it after binding!
Result:
Note that to avoid seeing the original lines shine through we set the BorderWidth to 0:
someSeries.BorderWidth = 0;
Simple way:
priceSerie.Points[index].Color = Color.Blue;
// priceSerie.Points[index].BorderColor = Color.Magenta;

winform mschart Column chart not centering column on datapoint

Hello I am having a hard time understanding how to get my column chart to be centered over the X-Axis label. IN the chart pic below you will see that I have columns based on 1/2 increments. For my positive series the data starts on the left and ends at the x-axis label. On my negative series the data starts at the x-axis label and continues to the right. How do I get these columns centered over the x-axis label. I don't understand why one series is displayed one way and the other series is displayed a different way.
Screen shot here:
http://i.imgur.com/QuAXUxr.png
Code is below
chartAreaDistributionHalfHour.Name = "ChartAreaDistributionHalfHour";
chartAreaDistributionHalfHour.AxisX.MajorGrid.LineColor = System.Drawing.Color.LightGray;
chartAreaDistributionHalfHour.AxisY.MajorGrid.LineColor = System.Drawing.Color.LightGray;
chartAreaDistributionHalfHour.AxisX.IntervalAutoMode = System.Windows.Forms.DataVisualization.Charting.IntervalAutoMode.VariableCount;
chartAreaDistributionHalfHour.AxisY.IntervalAutoMode = System.Windows.Forms.DataVisualization.Charting.IntervalAutoMode.VariableCount;
chartAreaDistributionHalfHour.AxisX.LabelAutoFitMaxFontSize = 7;
chartAreaDistributionHalfHour.AxisY.LabelAutoFitMaxFontSize = 7;
chartAreaDistributionHalfHour.AxisX.Title = "Distribution (Half-Hourly)";
chartAreaDistributionHalfHour.AxisY.Title = "Profit/Loss ($)";
chartAreaDistributionHalfHour.AxisY.IsMarginVisible = false;
seriesDistributionHalfHour.ChartArea = "ChartAreaDistributionHalfHour";
seriesDistributionHalfHour.ChartType = System.Windows.Forms.DataVisualization.Charting.SeriesChartType.Column;
seriesDistributionHalfHour.Color = System.Drawing.Color.Green;
seriesDistributionHalfHour.Enabled = true;
seriesDistributionHalfHour.Name = "distributionHalfHour";
seriesDistributionHalfHourNegative.ChartArea = "ChartAreaDistributionHalfHour";
seriesDistributionHalfHourNegative.ChartType = System.Windows.Forms.DataVisualization.Charting.SeriesChartType.Column;
seriesDistributionHalfHourNegative.Color = System.Drawing.Color.Red;
seriesDistributionHalfHourNegative.Enabled = true;
seriesDistributionHalfHourNegative.Name = "netLossDistributionHalfHour";
seriesDistributionHalfHourZeroLine.ChartArea = "ChartAreaDistributionHalfHour";
seriesDistributionHalfHourZeroLine.ChartType = System.Windows.Forms.DataVisualization.Charting.SeriesChartType.Line;
seriesDistributionHalfHourZeroLine.BorderDashStyle = System.Windows.Forms.DataVisualization.Charting.ChartDashStyle.Dash;
seriesDistributionHalfHourZeroLine.Color = System.Drawing.Color.Black;
seriesDistributionHalfHourZeroLine.Enabled = true;
seriesDistributionHalfHourZeroLine.Name = "netDistributionHalfHourZeroLine";
this.chartDistributionHalfHourGraph.Location = new System.Drawing.Point(17, 49);
this.chartDistributionHalfHourGraph.Name = "chartDistributionHalfHourGraph";
this.chartDistributionHalfHourGraph.BorderlineColor = System.Drawing.Color.Black;
this.chartDistributionHalfHourGraph.BorderlineDashStyle = System.Windows.Forms.DataVisualization.Charting.ChartDashStyle.Solid;
this.chartDistributionHalfHourGraph.Cursor = System.Windows.Forms.Cursors.Cross;
this.chartDistributionHalfHourGraph.Visible = false;
this.chartDistributionHalfHourGraph.Size = new System.Drawing.Size(1443, 694);
this.chartDistributionHalfHourGraph.TabIndex = 7;
this.chartDistributionHalfHourGraph.Text = "chart2";
this.chartDistributionHalfHourGraph.ChartAreas.Add(chartAreaDistributionHalfHour);
this.chartDistributionHalfHourGraph.Series.Add(seriesDistributionHalfHour);
this.chartDistributionHalfHourGraph.Series.Add(seriesDistributionHalfHourNegative);
this.chartDistributionHalfHourGraph.Series.Add(seriesDistributionHalfHourZeroLine);

What are the arrows on my mschart

Hello guys i have a MSChart and i have some arrows on chart exactly where the series are overlapping i would like to know at least what are they and maybe how can i get them out.
The chart is generated with two methods:
private void DesignChart()
{
// Create Chart Area
ChartArea chartArea1 = new ChartArea();
// Add Chart Area to the Chart
chart1.ChartAreas.Add(chartArea1);
chart1.Legends.Add(new Legend());
// chart1.ChartAreas[0].AxisX.LabelStyle.Format={"00:00:00"};
chart1.Location = new Point(14, 494);
chart1.Size = new Size(982, 224);
this.Controls.Add(chart1);
((System.ComponentModel.ISupportInitialize)(chart1)).EndInit();
}
private void SetGraph(){
.........
for (int i = 0; i < chart1.Series.Count; i++)
{
chart1.Series[i].XValueMember = "Date";
chart1.Series[i].YValueMembers = "Value";
chart1.Series[i].ChartType = System.Windows.Forms.DataVisualization.Charting.SeriesChartType.Line;
chart1.Series[i].XValueType = ChartValueType.Time;
chart1.Series[i].ChartType = SeriesChartType.Line;
chart1.Series[i].MarkerStyle = MarkerStyle.Star10;
chart1.Series[i].MarkerSize = 8;
chart1.Series[i].MarkerColor = chart1.Series[0].BorderColor;
chart1.Series[i].BorderWidth = 3;
chart1.Series[i].IsValueShownAsLabel = true;
chart1.Series[i].ToolTip = "#VALY => #AXISLABEL";
}
chart1.ChartAreas[0].AxisX.IsMarginVisible = true;
}
Picture link Photo of the chart
Thanks
I believe it happens because of this line:
chart1.Series[i].IsValueShownAsLabel = true;
If the data points are close together/overlap, chart control uses the arrow to point to the reference point so you don't get confused -- or something like that. Try setting this line to false and see if the arrows disappear. Not sure there is any other way to override the arrow display.
Is it possible the 'arrows' are actually the result of this line?
chart1.Series[i].MarkerStyle = MarkerStyle.Star10;

Categories