[vtkusers] vtkChartLegend Position in Python

Slaughter, Andrew E andrew.slaughter at inl.gov
Thu Oct 8 16:56:43 EDT 2015


That did the trick, I can do the math to make an axis based positioning.
Thanks for the help.

On Thu, Oct 8, 2015 at 2:21 PM, Cory Quammen <cory.quammen at kitware.com>
wrote:

> On Thu, Oct 8, 2015 at 3:58 PM, Slaughter, Andrew E <
> andrew.slaughter at inl.gov> wrote:
>
>>
>> On Thu, Oct 8, 2015 at 1:27 PM, Cory Quammen <cory.quammen at kitware.com>
>> wrote:
>>
>>> Well shoot, that member function must not be wrapped.
>>>
>>> Instead, try this
>>>
>>> legend = chart.GetLegend()
>>> legend.SetHorizontalAlignment(vtk.vtkChartLegend.CUSTOM)
>>> legend.SetVerticalAlignment(vtk.vtkChartLegend.CUSTOM)
>>> legend.SetPoint(0, 20)
>>>
>>
>> This causes the legend to move to the lower left in the window, SetPoint
>> does nothing.
>>
>
> (0, 20) is in the lower left of the window. Try with SetPoint(200, 200),
> for example. The legend should be in the upper right.
>
>
>> Using the Set(Horizontal/Vertical)Alignment methods move the legend to
>> the various points within the
>> plot window. For example, the following places it where I was hoping to
>> move the legend.
>> legend.SetHorizontalAlignment(vtk.vtkChartLegend.LEFT)
>> legend.SetVerticalAlignment(vtk.vtkChartLegend.TOP)
>>
>> However, this SetPoint method does not seem to change anything. Perhaps I
>> am just not understanding how the bounding box works for the object. I was
>> thinking that SetPoint would locate the corner of the legend box at the
>> specified point and that the Set(Horizontal/Vertical)Alignment methods
>> indicate how this box aligns to the point.
>>
>> The behavior seems to be that the legend has a bounding box equal to the
>> chart axis dimensions and that the alignment methods alter the placement of
>> the legend box within this larger bounding box.
>>
>
> Yes, the alignment refers to placement within the chart, not the alignment
> of the legend with respect to the position.
>
> - Cory
>
>
>>
>>
>>>
>> HTH,
>>> Cory
>>>
>>>
>>> On Thu, Oct 8, 2015 at 3:00 PM, Slaughter, Andrew E <
>>> andrew.slaughter at inl.gov> wrote:
>>>
>>>> Yes, that function does not exist.
>>>>
>>>> On Thu, Oct 8, 2015 at 12:45 PM, Cory Quammen <cory.quammen at kitware.com
>>>> > wrote:
>>>>
>>>>> Hi Andrew,
>>>>>
>>>>> Have you tried vtkChartXY::SetLegendPosition(const vtkRectf & rect),
>>>>> e.g.
>>>>>
>>>>> rect = vtkRectf(0, 20, ?, ?)
>>>>> char.SetLegendPosition(rect)
>>>>>
>>>>> HTH,
>>>>> Cory
>>>>>
>>>>> On Thu, Oct 8, 2015 at 2:33 PM, Slaughter, Andrew E <
>>>>> andrew.slaughter at inl.gov> wrote:
>>>>>
>>>>>> I am writing a script to build some line charts in VTK, but I am
>>>>>> having difficulty repositioning a vtkChartLegend object.
>>>>>>
>>>>>> The python script below will create a plot. I would like to
>>>>>> reposition the legend to the upper left-hand side of the chart so as to not
>>>>>> interfere with the line plot. However, I am not able to make the legend
>>>>>> move. I would appreciate any help to get the legend to re-position?
>>>>>>
>>>>>> I am running OSX (Yosemite) with Python 2.7.6 and VTK 6.3.
>>>>>>
>>>>>>
>>>>>> #!/usr/bin/env python
>>>>>> import vtk
>>>>>>
>>>>>> # Num. points
>>>>>> n = 10
>>>>>>
>>>>>> # Create some data
>>>>>> x = vtk.vtkFloatArray()
>>>>>> y = vtk.vtkFloatArray()
>>>>>> x.SetNumberOfTuples(n)
>>>>>> x.SetName('x')
>>>>>> y.SetNumberOfTuples(n)
>>>>>> y.SetName('y')
>>>>>>
>>>>>> for i in range(10):
>>>>>>     x.SetValue(i, float(i))
>>>>>>     y.SetValue(i, float(2*i))
>>>>>>
>>>>>> # Create the table and line
>>>>>> table = vtk.vtkTable()
>>>>>> table.AddColumn(x)
>>>>>> table.AddColumn(y)
>>>>>> line = vtk.vtkPlotLine()
>>>>>> line.SetInputData(table, 0, 1)
>>>>>>
>>>>>> # Create chart
>>>>>> chart = vtk.vtkChartXY()
>>>>>> chart.AddPlot(line)
>>>>>>
>>>>>> # Create view
>>>>>> view = vtk.vtkContextActor()
>>>>>> view.GetScene().AddItem(chart)
>>>>>>
>>>>>> # Renderer
>>>>>> renderer = vtk.vtkRenderer()
>>>>>> renderer.SetBackground(1,1,1)
>>>>>> renderer.AddViewProp(view)
>>>>>>
>>>>>> # Legend
>>>>>> chart.SetShowLegend(True)
>>>>>> legend = chart.GetLegend()
>>>>>> legend.SetPoint(0, 20) # This doesn't seem to do anything
>>>>>>
>>>>>> # Window
>>>>>> window = vtk.vtkRenderWindow()
>>>>>> window.AddRenderer(renderer)
>>>>>>
>>>>>> # Interactor
>>>>>> interactor = vtk.vtkRenderWindowInteractor()
>>>>>> interactor.SetRenderWindow(window)
>>>>>>
>>>>>> # Render
>>>>>> window.Render()
>>>>>> renderer.Render()
>>>>>> interactor.Start()
>>>>>>
>>>>>> _______________________________________________
>>>>>> Powered by www.kitware.com
>>>>>>
>>>>>> Visit other Kitware open-source projects at
>>>>>> http://www.kitware.com/opensource/opensource.html
>>>>>>
>>>>>> Please keep messages on-topic and check the VTK FAQ at:
>>>>>> http://www.vtk.org/Wiki/VTK_FAQ
>>>>>>
>>>>>> Search the list archives at: http://markmail.org/search/?q=vtkusers
>>>>>>
>>>>>> Follow this link to subscribe/unsubscribe:
>>>>>> http://public.kitware.com/mailman/listinfo/vtkusers
>>>>>>
>>>>>>
>>>>>
>>>>>
>>>>> --
>>>>> Cory Quammen
>>>>> R&D Engineer
>>>>> Kitware, Inc.
>>>>>
>>>>
>>>>
>>>
>>>
>>> --
>>> Cory Quammen
>>> R&D Engineer
>>> Kitware, Inc.
>>>
>>
>>
>
>
> --
> Cory Quammen
> R&D Engineer
> Kitware, Inc.
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://public.kitware.com/pipermail/vtkusers/attachments/20151008/bff58997/attachment.html>


More information about the vtkusers mailing list