Announcement

Collapse
No announcement yet.

Partner 728x90

Collapse

How to draw a line using the PlotStyle from a Plot?

Collapse
X
 
  • Filter
  • Time
  • Show
Clear All
new posts

    How to draw a line using the PlotStyle from a Plot?

    I want to extend a plot into the right margin space by drawing. how do I draw using the PlotStyle, if it is not a line. (e.g., PlotStyle.Cross)?

    #2
    Hello koganam,

    Thanks for your post. This would require custom rendering to accomplish. Below is a modified snippet from the help guide which can demonstrate drawing from the last bar to the right side margin.

    Code:
    protected override void OnRender(ChartControl chartControl, ChartScale chartScale)
    {
      base.OnRender(chartControl, chartScale);
      // get the starting and ending bars from what is rendered on the chart
      float startX = chartControl.GetXByBarIndex(ChartBars, ChartBars.ToIndex);
      float endX = (ChartPanel.X + ChartPanel.W);
    
      // Loop through each Plot Values on the chart
      for (int seriesCount = 0; seriesCount < Values.Length; seriesCount++)
      {
        // get the value at the last bar on the chart (if it has been set)
        if (Values[seriesCount].IsValidDataPointAt(ChartBars.ToIndex))
        {
            double plotValue = Values[seriesCount].GetValueAt(ChartBars.ToIndex);
    
            // convert the plot value to the charts "Y" axis point
            float chartScaleYValue = chartScale.GetYByValue(plotValue);
    
            // calculate the x and y values for the line to start and end
            SharpDX.Vector2 startPoint = new SharpDX.Vector2(startX, chartScaleYValue);
            SharpDX.Vector2 endPoint = new SharpDX.Vector2(endX, chartScaleYValue);
    
            // draw a line between the start and end point at each plot using the plots SharpDX Brush color and style
            RenderTarget.DrawLine(startPoint, endPoint, Plots[seriesCount].BrushDX,
              Plots[seriesCount].Width, Plots[seriesCount].StrokeStyle);
        }
      }
    }
    PlotStyle.Cross cannot be used as a SharpDX StrokeStyle, so additional vertical lines would need to be drawn the spanning from ChartBars.ToIndex to ChartPanel.X + CharPanel.W. You can draw small vertical lines on top of this bar spanning the distance between ChartBars.ToIndex and ChartBars.ToIndex-1 to offset each vertical line so they are distanced in the same fashion as other bars on the chart.

    With the small vertical lines drawn on top of the horizontal line, you will have achieved the same visual effect as having the PlotStyle.Cross plot extend to the right side margin.

    Please let me know if I can be of further assistance.
    JimNinjaTrader Customer Service

    Comment


      #3
      Originally posted by NinjaTrader_Jim View Post
      Hello koganam,

      Thanks for your post. This would require custom rendering to accomplish. Below is a modified snippet from the help guide which can demonstrate drawing from the last bar to the right side margin.

      Code:
      protected override void OnRender(ChartControl chartControl, ChartScale chartScale)
      {
      base.OnRender(chartControl, chartScale);
      // get the starting and ending bars from what is rendered on the chart
      float startX = chartControl.GetXByBarIndex(ChartBars, ChartBars.ToIndex);
      float endX = (ChartPanel.X + ChartPanel.W);
      
      // Loop through each Plot Values on the chart
      for (int seriesCount = 0; seriesCount < Values.Length; seriesCount++)
      {
      // get the value at the last bar on the chart (if it has been set)
      if (Values[seriesCount].IsValidDataPointAt(ChartBars.ToIndex))
      {
      double plotValue = Values[seriesCount].GetValueAt(ChartBars.ToIndex);
      
      // convert the plot value to the charts "Y" axis point
      float chartScaleYValue = chartScale.GetYByValue(plotValue);
      
      // calculate the x and y values for the line to start and end
      SharpDX.Vector2 startPoint = new SharpDX.Vector2(startX, chartScaleYValue);
      SharpDX.Vector2 endPoint = new SharpDX.Vector2(endX, chartScaleYValue);
      
      // draw a line between the start and end point at each plot using the plots SharpDX Brush color and style
      RenderTarget.DrawLine(startPoint, endPoint, Plots[seriesCount].BrushDX,
      Plots[seriesCount].Width, Plots[seriesCount].StrokeStyle);
      }
      }
      }
      PlotStyle.Cross cannot be used as a SharpDX StrokeStyle, so additional vertical lines would need to be drawn the spanning from ChartBars.ToIndex to ChartPanel.X + CharPanel.W. You can draw small vertical lines on top of this bar spanning the distance between ChartBars.ToIndex and ChartBars.ToIndex-1 to offset each vertical line so they are distanced in the same fashion as other bars on the chart.

      With the small vertical lines drawn on top of the horizontal line, you will have achieved the same visual effect as having the PlotStyle.Cross plot extend to the right side margin.

      Please let me know if I can be of further assistance.
      Your response goes way further afield, and has a specificity that is way too narrow, relative to the question that I asked. I have already been able to finagle the PlotStyle.Cross pretty much in the manner that you describe. However, I used that PlotStyle pretty much as an example for my question, not as the only specific style to be used. I wanted to know how to use the Plot's PlotStyle, whatever it may be. After all, you are rendering the chart with the various plot styles, per user choice.

      I was asking how to do the same using whatever the user may have already chosen. I really did not want to have to think through and code for each specific choice in the enumeration. I am way too lazy for that.

      Comment

      Latest Posts

      Collapse

      Topics Statistics Last Post
      Started by Kaiviti57, Today, 04:13 AM
      0 responses
      1 view
      0 likes
      Last Post Kaiviti57  
      Started by Cross161, Yesterday, 10:33 PM
      0 responses
      10 views
      0 likes
      Last Post Cross161  
      Started by neilzep, Yesterday, 08:55 PM
      0 responses
      8 views
      0 likes
      Last Post neilzep
      by neilzep
       
      Started by javier.filgueira, Yesterday, 03:29 PM
      3 responses
      11 views
      0 likes
      Last Post javier.filgueira  
      Started by Da vinci, Yesterday, 03:15 PM
      1 response
      8 views
      0 likes
      Last Post NinjaTrader_ChrisL  
      Working...
      X