// // Draw the track line with legend // private void crossHair(XYChart c, int mouseX, int mouseY) { //System.Threading.Thread.Sleep(500); // Clear the current dynamic layer and get the DrawArea object to draw on it. DrawArea d = c.initDynamicLayer(); // The plot area object PlotArea plotArea = c.getPlotArea(); // Draw a vertical line and a horizontal line as the cross hair d.vline(plotArea.getTopY(), plotArea.getBottomY(), mouseX, d.dashLineColor(0x000000, 0x0101)); d.hline(plotArea.getLeftX(), plotArea.getRightX(), mouseY, d.dashLineColor(0x000000, 0x0101)); // Draw y-axis label string label = "<*block,bgColor=FFFFDD,margin=3,edgeColor=000000*>" + c.formatValue(c.getYValue( mouseY, c.yAxis()), "{value|P4}") + "<*/*>"; TTFText t = d.text(label, "Arial Bold", 8); t.draw(plotArea.getLeftX() - 5, mouseY, 0x000000, Chart.Right); // Draw x-axis label label = "<*block,bgColor=FFFFDD,margin=3,edgeColor=000000*>" + c.formatValue(c.getXValue(mouseX), "{value|P4}") + "<*/*>"; t = d.text(label, "Arial Bold", 8); t.draw(mouseX, plotArea.getBottomY() + 5, 0x000000, Chart.Top); }
// // Draw track cursor when mouse is moving over plotarea // private void winChartViewer1_MouseMovePlotArea(object sender, MouseEventArgs e) { var viewer = (WinChartViewer)sender; // Mouse can drag the track lines if it is in scroll mode var mouseUsage = viewer.MouseUsage; if (((mouseUsage != WinChartMouseUsage.ScrollOnDrag) && (mouseUsage != WinChartMouseUsage.Default)) || (trackLinePos.Count == 0)) { return; } int mouseX = viewer.ChartMouseX; // Check if mouse button is down if ((MouseButtons & MouseButtons.Left) != 0) { // If mouse is near track line, then it is dragging the track line if (nearestTrackLine >= 0) { XYChart c = (XYChart)viewer.Chart; PlotArea p = c.getPlotArea(); // move the track line while ensuring the track line is in the plot area trackLinePos[nearestTrackLine] = Math.Min(p.getRightX(), Math.Max(p.getLeftX(), mouseX - nearestDistance)); // repaint the track lines trackLineLabel(c); viewer.updateDisplay(); } } else { // Check which track line is nearest to the mouse nearestTrackLine = -1; nearestDistance = 9; for (int i = 0; i < trackLinePos.Count; ++i) { if (Math.Abs(mouseX - trackLinePos[i]) < Math.Abs(nearestDistance)) { nearestTrackLine = i; nearestDistance = mouseX - trackLinePos[i]; } } // If mouse is near the track line, it is used to drag the line, so disable drag to scroll. viewer.MouseUsage = ((nearestTrackLine >= 0) ? WinChartMouseUsage.Default : WinChartMouseUsage.ScrollOnDrag); } }
void drawTrackLine(XYChart c, int lineX, Dictionary <string, double> log) { // The drawarea and plotarea objects DrawArea d = c.getDrawArea(); PlotArea plotArea = c.getPlotArea(); // Get the data x-value that is nearest to the mouse, and find its pixel coordinate. double xValue = c.getNearestXValue(lineX); int xCoor = c.getXCoor(xValue); // Draw empty track line if it is ahead of the data if ((currentIndex <= 0) || ((xCoor < lineX) && (xValue >= chartTimeLimit))) { d.vline(plotArea.getTopY(), plotArea.getBottomY(), lineX, 0x888888); return; } // Draw a vertical track line at the x-position d.vline(plotArea.getTopY(), plotArea.getBottomY(), xCoor, 0x888888); // Draw a label on the x-axis to show the track line position. string xlabel = "<*font,bgColor=000000*> " + c.xAxis().getFormattedLabel(xValue, "nn:ss.ff") + " <*/font*>"; TTFText t = d.text(xlabel, "Arial Bold", 10); log["x"] = xValue; // Restrict the x-pixel position of the label to make sure it stays inside the chart image. int xLabelPos = Math.Max(0, Math.Min(xCoor - t.getWidth() / 2, c.getWidth() - t.getWidth())); t.draw(xLabelPos, plotArea.getBottomY() + 6, 0xffffff); // Iterate through all layers to draw the data labels for (int i = 0; i < c.getLayerCount(); ++i) { Layer layer = c.getLayerByZ(i); // The data array index of the x-value int xIndex = layer.getXIndexOf(xValue); // Iterate through all the data sets in the layer for (int j = 0; j < layer.getDataSetCount(); ++j) { ChartDirector.DataSet dataSet = layer.getDataSetByZ(j); // Get the color and position of the data label int color = dataSet.getDataColor(); int yCoor = c.getYCoor(dataSet.getPosition(xIndex), dataSet.getUseYAxis()); // Draw a track dot with a label next to it for visible data points in the plot area if ((yCoor >= plotArea.getTopY()) && (yCoor <= plotArea.getBottomY()) && (color != Chart.Transparent) && (!string.IsNullOrEmpty(dataSet.getDataName()))) { d.circle(xCoor, yCoor, 4, 4, color, color); string label = "<*font,bgColor=" + color.ToString("x") + "*> " + c.formatValue( dataSet.getValue(xIndex), "{value|P4}") + " <*/font*>"; t = d.text(label, "Arial Bold", 10); log[dataSet.getDataName()] = dataSet.getValue(xIndex); // Draw the label on the right side of the dot if the mouse is on the left side the // chart, and vice versa. This ensures the label will not go outside the chart image. if (xCoor <= (plotArea.getLeftX() + plotArea.getRightX()) / 2) { t.draw(xCoor + 5, yCoor, 0xffffff, Chart.Left); } else { t.draw(xCoor - 5, yCoor, 0xffffff, Chart.Right); } } } } }
// // Draw track line with data labels // private void trackLineLabel(XYChart c, int mouseX) { // Clear the current dynamic layer and get the DrawArea object to draw on it. DrawArea d = c.initDynamicLayer(); // The plot area object PlotArea plotArea = c.getPlotArea(); // Get the data x-value that is nearest to the mouse, and find its pixel coordinate. double xValue = c.getNearestXValue(mouseX); int xCoor = c.getXCoor(xValue); // Draw a vertical track line at the x-position d.vline(plotArea.getTopY(), plotArea.getBottomY(), xCoor, d.dashLineColor(0x000000, 0x0101)); // Draw a label on the x-axis to show the track line position. string xlabel = "<*font,bgColor=000000*> " + c.xAxis().getFormattedLabel(xValue, "mmm dd, yyyy") + " <*/font*>"; TTFText t = d.text(xlabel, "Arial Bold", 8); // Restrict the x-pixel position of the label to make sure it stays inside the chart image. int xLabelPos = Math.Max(0, Math.Min(xCoor - t.getWidth() / 2, c.getWidth() - t.getWidth())); t.draw(xLabelPos, plotArea.getBottomY() + 6, 0xffffff); // Iterate through all layers to draw the data labels for (int i = 0; i < c.getLayerCount(); ++i) { Layer layer = c.getLayerByZ(i); // The data array index of the x-value int xIndex = layer.getXIndexOf(xValue); // Iterate through all the data sets in the layer for (int j = 0; j < layer.getDataSetCount(); ++j) { ChartDirector.DataSet dataSet = layer.getDataSetByZ(j); // Get the color and position of the data label int color = dataSet.getDataColor(); int yCoor = c.getYCoor(dataSet.getPosition(xIndex), dataSet.getUseYAxis()); // Draw a track dot with a label next to it for visible data points in the plot area if ((yCoor >= plotArea.getTopY()) && (yCoor <= plotArea.getBottomY()) && (color != Chart.Transparent)) { d.circle(xCoor, yCoor, 4, 4, color, color); string label = "<*font,bgColor=" + color.ToString("x") + "*> " + c.formatValue( dataSet.getValue(xIndex), "{value|P4}") + " <*/font*>"; t = d.text(label, "Arial Bold", 8); // Draw the label on the right side of the dot if the mouse is on the left side the // chart, and vice versa. This ensures the label will not go outside the chart image. if (xCoor <= (plotArea.getLeftX() + plotArea.getRightX()) / 2) { t.draw(xCoor + 5, yCoor, 0xffffff, Chart.Left); } else { t.draw(xCoor - 5, yCoor, 0xffffff, Chart.Right); } } } } }