.. DO NOT EDIT. .. THIS FILE WAS AUTOMATICALLY GENERATED BY SPHINX-GALLERY. .. TO MAKE CHANGES, EDIT THE SOURCE PYTHON FILE: .. "examples/030-create-and-modify-charts.py" .. LINE NUMBERS ARE GIVEN BELOW. .. only:: html .. note:: :class: sphx-glr-download-link-note :ref:`Go to the end ` to download the full example code. .. rst-class:: sphx-glr-example-title .. _sphx_glr_examples_030-create-and-modify-charts.py: .. _create_and_modify_charts_example: Create and modify charts ======================== This example demonstrates how to create and work with charts in Result Explorer: - **Chart creation** using the ``ChartDefinition`` and ``ChartResult`` objects to define result series with filters and field selections. - **Multi-result charts** combining equivalent stress, temperature, and contact pressure results in a single chart. - **Chart display options** to control legend visibility, table display, and active series selection. - **Chart updates** to add or modify result series in an existing chart. - **Snapshot capture** to save chart visualizations as images. .. GENERATED FROM PYTHON SOURCE LINES 37-38 Import the Result Explorer dependencies. .. GENERATED FROM PYTHON SOURCE LINES 38-54 .. code-block:: Python from ansys.result_explorer.core import ( ChartDefinition, ChartResult, Field, Filter, ResultFieldName, ResultType, ShellPosition, launch_result_explorer, ) from ansys.result_explorer.core.examples import ( ExampleKeys, get_example_file, get_example_snapshot_settings, ) .. GENERATED FROM PYTHON SOURCE LINES 55-58 Launch Result Explorer ---------------------- Start a Result Explorer instance for this example. .. GENERATED FROM PYTHON SOURCE LINES 58-60 .. code-block:: Python rx = launch_result_explorer() .. GENERATED FROM PYTHON SOURCE LINES 61-66 Load example data ----------------- Create a solution from a transient analysis result file with multiple time steps. Print the solution to inspect available results and configurable chart types. .. GENERATED FROM PYTHON SOURCE LINES 66-76 .. code-block:: Python rst_path = get_example_file(ExampleKeys.RST_CP_TRANSIENT) sol = rx.create_solution( name="Chart Example Solution", file_path=rst_path, ) print(f"Created solution: {sol.name}") print(f" Time steps: {sol.n_sets}") print(sol) .. rst-class:: sphx-glr-script-out .. code-block:: none Created solution: Chart Example Solution Time steps: 15 ====================================================================== Solution: Chart Example Solution ====================================================================== ID: c41ea68b-2590-4038-af2f-25389206ec48 Description: N/A Status: Ready: Yes Live: No Result Files: - /home/runner/work/pyresultexplorer/pyresultexplorer/tests/data/cp_trans/file.rst (key: rst) Analysis Information: Physics Type: mechanical Analysis Type: transient Solver Version: 24.2 Unit System: MKS: m, kg, N, s, V, A, degC Mesh Information: Num Elements: 122 Num Nodes: 406 Num Named Selections: 13 Num Bodies: 10 Distance Unit: m Results Information: Num Sets: 15 Available Results: 24 Time/Freq Unit: s Available Plot Results: Displacement - total_displacement - displacement [X, Y, Z] Stress - stress_tensor [XX, YY, ZZ, XY, YZ, XZ] - equivalent_von_mises_stress - principal_stress_1 [1] - principal_stress_2 [2] - principal_stress_3 [3] Velocity - total_velocity - velocity [X, Y, Z] Acceleration - total_acceleration - acceleration [X, Y, Z] Temperature - temperature Heat Flux - total_heat_flux - heat_flux [X, Y, Z] Structural Temperature - structural_temperature Contact - contact_status - contact_pressure - contact_penetration - contact_sliding_distance - contact_gap_distance - contact_friction_stress - contact_total_stress - contact_fluid_penetration_pressure - contact_surface_heat_flux Newton Raphson Residual - total_force - total_moment User Defined Solver Text Outputs (Logs): - solve.out - file.err - file.gst - file.cnd ====================================================================== .. GENERATED FROM PYTHON SOURCE LINES 77-79 Create a workspace ------------------- .. GENERATED FROM PYTHON SOURCE LINES 79-81 .. code-block:: Python workspace = rx.create_workspace(name="Chart Example Workspace") .. GENERATED FROM PYTHON SOURCE LINES 82-87 Create a chart --------------- Build a chart that plots equivalent von Mises stress, temperature, and contact pressure over all time steps. ``Filter.max`` keeps only the maximum value per step so the chart shows peak results. .. GENERATED FROM PYTHON SOURCE LINES 87-121 .. code-block:: Python chart = sol.create_chart( ChartDefinition( name="Stress, Temperature & Contact Pressure Over Time", all_sets=True, results=[ ChartResult( result_type=ResultType.stress, name="Stress", location="Nodal", fields=[Field(ResultFieldName.equivalent_von_mises_stress)], filters=[Filter.max], shell_position=ShellPosition.all, ), ChartResult( result_type=ResultType.temperature, name="Temperature", location="Nodal", fields=[Field(ResultFieldName.temperature)], filters=[Filter.max], ), ChartResult( result_type=ResultType.contact, name="Contact", location="Nodal", fields=[Field(ResultFieldName.contact_pressure)], filters=[Filter.max], ), ], ) ) print(f"\nCreated chart view: '{chart.name}'") print(f"Chart definition id: {chart.definition.id}") .. rst-class:: sphx-glr-script-out .. code-block:: none Created chart view: 'Stress, Temperature & Contact Pressure Over Time' Chart definition id: 1e1fecc7-783a-422e-89d6-9aebdddd76a0 .. GENERATED FROM PYTHON SOURCE LINES 122-125 Assign the chart to a viewport -------------------------------- Assign the chart view to a viewport and wait for it to finish rendering. .. GENERATED FROM PYTHON SOURCE LINES 125-128 .. code-block:: Python viewport = workspace.assign_view(view=chart, wait=True) print(f"\nViewport assigned: {viewport.id}") .. rst-class:: sphx-glr-script-out .. code-block:: none Viewport assigned: 7db10ccf-f959-4f18-9be3-e650cb684d28 .. GENERATED FROM PYTHON SOURCE LINES 129-133 Inspect chart display options ------------------------------ Read the available series and chart names provided by the server after rendering, then print them so you can reference them by name. .. GENERATED FROM PYTHON SOURCE LINES 133-139 .. code-block:: Python opts = viewport.display_options print(f"\nAvailable series: {opts.series_names}") print(f"Available charts: {opts.chart_names}") print(f"Active series: {opts.active_series}") .. rst-class:: sphx-glr-script-out .. code-block:: none Available series: ['Time/Frequency', 'Stress: Max Equivalent Von Mises Stress', 'Temperature: Max Temperature', 'Contact: Max Contact Pressure'] Available charts: ['Stress, Temperature & Contact Pressure Over Time'] Active series: ['Stress: Max Equivalent Von Mises Stress', 'Temperature: Max Temperature', 'Contact: Max Contact Pressure'] .. GENERATED FROM PYTHON SOURCE LINES 140-143 Configure display options -------------------------- Enable the legend and show the data table beneath the chart. .. GENERATED FROM PYTHON SOURCE LINES 143-146 .. code-block:: Python opts.show_legend = True opts.show_table = True .. GENERATED FROM PYTHON SOURCE LINES 147-151 Select active series --------------------- Restrict the viewport to display only the stress series so temperature and contact pressure are hidden. .. GENERATED FROM PYTHON SOURCE LINES 151-160 .. code-block:: Python if opts.series_names: opts.active_series = opts.series_names[1:2] print(f"\nActive series (stress only): {opts.active_series}") viewport.save_snapshot( file_path="030-chart-stress-only.png", settings=get_example_snapshot_settings(), ) .. image-sg:: /examples/images/sphx_glr_030-create-and-modify-charts_001.png :alt: 030 create and modify charts :srcset: /examples/images/sphx_glr_030-create-and-modify-charts_001.png :class: sphx-glr-single-img .. rst-class:: sphx-glr-script-out .. code-block:: none Active series (stress only): ['Stress: Max Equivalent Von Mises Stress'] .. GENERATED FROM PYTHON SOURCE LINES 161-164 Restore all series ------------------- Re-activate all available series. .. GENERATED FROM PYTHON SOURCE LINES 164-171 .. code-block:: Python opts.active_series = opts.series_names[1:] viewport.save_snapshot( file_path="030-chart-with-all-quantities.png", settings=get_example_snapshot_settings(), ) .. image-sg:: /examples/images/sphx_glr_030-create-and-modify-charts_002.png :alt: 030 create and modify charts :srcset: /examples/images/sphx_glr_030-create-and-modify-charts_002.png :class: sphx-glr-single-img .. GENERATED FROM PYTHON SOURCE LINES 172-175 List charts in the solution ---------------------------- ``sol.charts`` now returns a list of native ``ChartDefinition`` objects. .. GENERATED FROM PYTHON SOURCE LINES 175-180 .. code-block:: Python print("\nCharts in solution:") for c in sol.charts: print(f" {c.name!r} — {len(c.results)} result series") rx.stop() .. rst-class:: sphx-glr-script-out .. code-block:: none Charts in solution: 'Min/Max Displacement Over Time' — 1 result series 'Min/Max Equivalent Stress Over Time' — 1 result series 'Temperature Over Time' — 1 result series 'Stress, Temperature & Contact Pressure Over Time' — 3 result series .. rst-class:: sphx-glr-timing **Total running time of the script:** (0 minutes 21.503 seconds) .. _sphx_glr_download_examples_030-create-and-modify-charts.py: .. only:: html .. container:: sphx-glr-footer sphx-glr-footer-example .. container:: sphx-glr-download sphx-glr-download-jupyter :download:`Download Jupyter notebook: 030-create-and-modify-charts.ipynb <030-create-and-modify-charts.ipynb>` .. container:: sphx-glr-download sphx-glr-download-python :download:`Download Python source code: 030-create-and-modify-charts.py <030-create-and-modify-charts.py>` .. container:: sphx-glr-download sphx-glr-download-zip :download:`Download zipped: 030-create-and-modify-charts.zip <030-create-and-modify-charts.zip>` .. only:: html .. rst-class:: sphx-glr-signature `Gallery generated by Sphinx-Gallery `_