CommandChartLine.java
/* ========================================================================
* PlantUML : a free UML diagram generator
* ========================================================================
*
* (C) Copyright 2009-2024, Arnaud Roques
*
* Project Info: https://plantuml.com
*
* If you like this project or if you find it useful, you can support us at:
*
* https://plantuml.com/patreon (only 1$ per month!)
* https://plantuml.com/paypal
*
* This file is part of PlantUML.
*
* PlantUML is free software; you can redistribute it and/or modify it
* under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* PlantUML distributed in the hope that it will be useful, but
* WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
* or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public
* License for more details.
*
* You should have received a copy of the GNU General Public
* License along with this library; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301,
* USA.
*
*
* Original Author: David Fyfe
*
*/
package net.sourceforge.plantuml.chart.command;
import java.util.Arrays;
import java.util.Collection;
import net.sourceforge.plantuml.annotation.Explain;
import net.sourceforge.plantuml.chart.ChartDiagram;
import net.sourceforge.plantuml.chart.ChartSeries;
import net.sourceforge.plantuml.command.CommandExecutionResult;
import net.sourceforge.plantuml.command.ParserPass;
import net.sourceforge.plantuml.command.SingleLineCommand2;
import net.sourceforge.plantuml.klimt.color.HColor;
import net.sourceforge.plantuml.klimt.color.NoSuchColorException;
import net.sourceforge.plantuml.regex.IRegex;
import net.sourceforge.plantuml.regex.RegexConcat;
import net.sourceforge.plantuml.regex.RegexLeaf;
import net.sourceforge.plantuml.regex.RegexOptional;
import net.sourceforge.plantuml.regex.RegexResult;
import net.sourceforge.plantuml.stereo.Stereotype;
import net.sourceforge.plantuml.utils.LineLocation;
public class CommandChartLine extends SingleLineCommand2<ChartDiagram> {
private static final Collection<String> FIRST_TOKENS = Arrays.asList("line");
public CommandChartLine() {
super(false, getRegexConcat());
}
static IRegex getRegexConcat() {
return RegexConcat.build(CommandChartLine.class.getName(), RegexLeaf.start(), //
RegexLeaf.spaceZeroOrMore(), //
new RegexLeaf("line"), //
RegexLeaf.spaceZeroOrMore(), //
new RegexOptional(new RegexLeaf(1, "STEREO", "(\\<\\<.+?\\>\\>)")), //
RegexLeaf.spaceZeroOrMore(), //
new RegexOptional(ChartSeriesName.regex("v2", "y2", "labels", "interpolate", "connect")), //
RegexLeaf.spaceZeroOrMore(), //
new RegexOptional(new RegexLeaf(1, "DATA", ChartDataItems.BRACKETED)), //
RegexLeaf.spaceZeroOrMore(), //
new RegexOptional(new RegexLeaf(1, "COLOR", "#([0-9a-fA-F]{6}|[0-9a-fA-F]{3}|\\w+)")), //
new RegexOptional( //
new RegexConcat( //
RegexLeaf.spaceOneOrMore(), //
new RegexLeaf(1, "V2", "([vy]2)"))), //
new RegexOptional( //
new RegexConcat( //
RegexLeaf.spaceOneOrMore(), //
new RegexLeaf(1, "LABELS", "(labels)"))), //
new RegexOptional( //
new RegexConcat( //
RegexLeaf.spaceOneOrMore(), //
new RegexLeaf(1, "INTERPOLATE", "(interpolate)"))), //
new RegexOptional( //
new RegexConcat( //
RegexLeaf.spaceOneOrMore(), //
new RegexLeaf(1, "CONNECT", "(connect)"))), //
RegexLeaf.spaceZeroOrMore(), //
RegexLeaf.end());
}
@Override
@Explain
protected String explainArg(LineLocation location, RegexResult arg) {
return null;
}
@Override
protected CommandExecutionResult executeArg(ChartDiagram diagram, LineLocation location, RegexResult arg,
ParserPass currentPass) throws NoSuchColorException {
final String stereo = arg.get("STEREO", 0);
final String name = ChartSeriesName.get(arg);
final String data = arg.get("DATA", 0);
final String colorStr = arg.get("COLOR", 0);
final ChartSeries series;
final String seriesName = name != null ? name : "line" + diagram.getSeries().size();
final ChartCartesianData parsed = ChartCartesianData.parse(data, true);
if (parsed.getError() != null)
return CommandExecutionResult.error(parsed.getError());
if (parsed.isPairs())
series = new ChartSeries(seriesName, ChartSeries.SeriesType.LINE, parsed.getXValues(),
parsed.getValues());
else
series = new ChartSeries(seriesName, ChartSeries.SeriesType.LINE, parsed.getValues());
if (parsed.isKeyed())
series.setKeys(parsed.getKeys());
if (stereo != null) {
series.setStereotype(Stereotype.build(stereo));
}
if (colorStr != null) {
final HColor color = diagram.getSkinParam().getIHtmlColorSet().getColor("#" + colorStr);
series.setColor(color);
}
// Check if this line should use the secondary v-axis
final String secondaryAxisStr = arg.get("V2", 0);
if (secondaryAxisStr != null) {
series.setUseSecondaryAxis(true);
}
// Check if labels keyword was present
final String labelsStr = arg.get("LABELS", 0);
if (labelsStr != null) {
series.setShowLabels(true);
}
// Check if the line should be a smooth curve through its points
if (arg.get("INTERPOLATE", 0) != null)
series.setInterpolate(true);
// Check if the line should join the values on both sides of a missing one
if (arg.get("CONNECT", 0) != null)
series.setConnect(true);
// Written without data, the series takes its values from the list items that follow it
if (data == null)
return diagram.openSeries(series);
return diagram.addSeries(series);
}
@Override
public Collection<String> mandatoryFirstTokensFast() {
return FIRST_TOKENS;
}
}