• If this is your first visit, you will have to register before you can post. To view messages, please scroll below and select the forum that you would like to visits. Questions? Be sure to check out the Forum FAQ.

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 MARTIN MARTIN, Yesterday, 01:01 PM
      3 responses
      39 views
      0 likes
      Last Post MARTIN MARTIN  
      Started by NickyD, Today, 11:42 AM
      1 response
      9 views
      0 likes
      Last Post NinjaTrader_Jim  
      Started by cupir2, Today, 11:25 AM
      3 responses
      15 views
      0 likes
      Last Post NinjaTrader_Jesse  
      Started by 7robert, Today, 11:15 AM
      2 responses
      7 views
      0 likes
      Last Post 7robert
      by 7robert
       
      Started by meandthetrio, Today, 10:52 AM
      0 responses
      11 views
      0 likes
      Last Post meandthetrio  
      Working...
      X