matlab.graphics.chartcontainer.ChartContainer.getTheme - Get figure theme of chart container subclass - MATLAB (original) (raw)
Main Content
Class: matlab.graphics.chartcontainer.ChartContainer
Namespace: matlab.graphics.chartcontainer
Get figure theme of chart container subclass
Since R2025a
Syntax
Description
[gt](#mw%5F5968f44b-79de-4172-a900-315d337bdc55) = getTheme([obj](#mw%5F4c6b28af-4710-4d64-8be4-2c3ed8934040))
returns the GraphicsTheme
object for the parent figure of the specifiedChartContainer
object. To determine the theme of the figure, query theBaseColorStyle
property of gt
.
The update
method of the chart container executes when the figure theme changes, so you can call this method from within the update
method of your subclass to respond to changes in the figure theme.
Input Arguments
Chart container, specified as an object of the class that inherits from thematlab.graphics.chartcontainer.ChartContainer
base class.
Output Arguments
Graphics theme information, returned as a GraphicsTheme
object that has the properties in this table.
Property | Description |
---|---|
Name | Short description of the theme, returned as 'Light Theme' or'Dark Theme'. |
BaseColorStyle | Basic color style of the theme, returned as 'light' or'dark'. This property indicates the overall lightness or darkness of the theme. Query this property when deciding on a color palette for your chart or when writing a ThemeChangedFcn callback. |
Examples
Define a class named ThemedSmoothPlot
that plots a set of data using a faint line as well as a smoothed version of the same data using a bolder line.
To define the class, create a file named ThemedSmoothPlot.m
that contains the following class definition with these features:
XData
andYData
public properties that store the _x_- and _y_-coordinate data for the original lineSmoothWidth
public property that controls the width of the smooth lineOriginalLine
andSmoothLine
private properties that store theLine
objects for the original and smoothed data- A
setup
method that initializesOriginalLine
andSmoothLine
- An
update
method that updates the plot when the user changes the value of a property - A
createSmoothData
method that calculates a smoothed version ofYData
- An
updateColor
method that selects plot colors based on the theme of the figure
classdef ThemedSmoothPlot < matlab.graphics.chartcontainer.ChartContainer properties XData (1,:) double = NaN YData (1,:) double = NaN SmoothWidth (1,1) double = 2 end properties(Access = private,Transient,NonCopyable) OriginalLine (1,1) matlab.graphics.chart.primitive.Line SmoothLine (1,1) matlab.graphics.chart.primitive.Line end
methods(Access = protected)
function setup(obj)
% Create the axes
ax = getAxes(obj);
% Create the original and smooth lines
obj.OriginalLine = plot(ax,NaN,NaN);
hold(ax,"on")
obj.SmoothLine = plot(ax,NaN,NaN);
hold(ax,"off")
end
function update(obj)
% Update line data
obj.OriginalLine.XData = obj.XData;
obj.OriginalLine.YData = obj.YData;
obj.SmoothLine.XData = obj.XData;
obj.SmoothLine.YData = createSmoothData(obj);
% Update line colors and smooth line width
updateColor(obj);
obj.SmoothLine.LineWidth = obj.SmoothWidth;
end
function sm = createSmoothData(obj)
% Calculate smoothed data
v = ones(1,10)*0.1;
sm = conv(obj.YData,v,"same");
end
function updateColor(obj)
OriginalLineColor = [0.6 0.7 1];
SmoothLineColor = [0.3 0.4 0.7];
thm = getTheme(obj);
switch thm.BaseColorStyle
case "light"
% Use OriginalLineColor and SmoothLineColor
obj.OriginalLine.Color = OriginalLineColor;
obj.SmoothLine.Color = SmoothLineColor;
case "dark"
% Flip OriginalLineColor and SmoothLineColor
obj.OriginalLine.Color = fliplightness(OriginalLineColor);
obj.SmoothLine.Color = fliplightness(SmoothLineColor);
end
end
end
end
Create a light-themed figure. Then plot pair of x
and y
vectors by calling the ThemedSmoothPlot
constructor method and specifying the XData
and YData
name-value arguments.
f = figure(Theme="light"); f.Position(3:4) = [695 420]; x = 1:1:100; y = 10sin(x./5) + 8sin(10.*x + 0.5); ThemedSmoothPlot(XData=x,YData=y);
Change the theme of the figure to "dark"
. The line colors update in response.
Version History
Introduced in R2025a