diff --git a/Modules/Chart/resource/Chart.js b/Modules/Chart/resource/Chart.js index 8f6a54804c..08ecb25e4d 100644 --- a/Modules/Chart/resource/Chart.js +++ b/Modules/Chart/resource/Chart.js @@ -1,549 +1,576 @@ document.body.style.backgroundColor = 'rgb(240, 240, 240)'; const minHeight = 255; var chart; var xErrorValuesPlus=[]; var xErrorValuesMinus=[]; var yErrorValuesPlus=[]; var yErrorValuesMinus=[]; var xValues=[]; var yValues=[]; var dataLabels=[]; var xs = {}; var dataColors = {}; var chartTypes = {}; var lineStyle = {}; var backgroundColor = '#f0f0f0'; var foregroundColor = 'black'; var dataProperties = {}; var chartData = null; // Important loading function. This will be executed at first in this whole script. // Fetching data from QWebChannel and storing them for display purposes. window.onload = function() { initHeight(); /** * Adds handler for Qt signal emitted from QmitkChartxyData. * Changes for single traces like value changes in a line plot are handled here. */ function handleDataChangeEvents(registeredChannelObject) { let position = registeredChannelObject.m_LabelCount; registeredChannelObject.SignalDiagramTypeChanged.connect(function(newValue){ console.log("diagram type changed"); console.log(newValue); let updateDiagramType = generateTraceByChartType(newValue); Plotly.restyle('chart', updateDiagramType, position); }); registeredChannelObject.SignalLineStyleChanged.connect(function(newValue){ console.log("line style changed"); console.log(newValue); var dashValue; if (newValue == "dashed"){ dashValue = "dot"; } else { dashValue = "solid"; } updateDash = { line : { "dash" : dashValue } } Plotly.restyle('chart', updateDash, position); }); registeredChannelObject.SignalColorChanged.connect(function(newValue){ var updateColor={ marker:{ "color" : newValue }, line:{ "color" : newValue } } Plotly.restyle('chart', updateColor, position); }); registeredChannelObject.SignalXDataChanged.connect(function(newValue){ console.log("xdata changed"); }); registeredChannelObject.SignalYDataChanged.connect(function(newValue){ console.log("ydata changed"); }); } /** * Adds handler for Qt signal emitted from QmitkChartData. * Changes for the whole chart like title are handled here. */ function handleChartChangeEvents(registeredChannelObject) { registeredChannelObject.SignalXAxisLabelChanged.connect(function(newValue){ var layout = { xaxis: { title: { text: newValue }, color: foregroundColor } } Plotly.relayout('chart', layout); }); registeredChannelObject.SignalYAxisLabelChanged.connect(function(newValue){ var layout = { yaxis: { title: { text: newValue }, color: foregroundColor } } Plotly.relayout('chart', layout); }); registeredChannelObject.SignalTitleChanged.connect(function(newValue){ var layout = { title: { text:newValue, font: { color: foregroundColor } } } Plotly.relayout('chart', layout); }); registeredChannelObject.SignalLegendPositionChanged.connect(function(newValue){ let legendPosition = generateLegendPosition(chartData.m_LegendPosition); var layout = { legend: legendPosition } Plotly.relayout('chart', layout); }); + + registeredChannelObject.SignalYAxisScaleChanged.connect(function(newValue){ + var layout = { + yaxis : { + type : newValue + } + }; + Plotly.relayout('chart', layout); + }); + + registeredChannelObject.SignalShowLegendChanged.connect(function(newValue){ + var layout = { + showlegend : newValue + }; + Plotly.relayout('chart', layout); + }); + + registeredChannelObject.SignalShowSubchartChanged.connect(function(newValue){ + var layout = { + xaxis : {} + }; + if (newValue){ + layout.xaxis.rangeslider = {}; // adds range slider below x axis + } + Plotly.relayout('chart', layout); + }); + } new QWebChannel(qt.webChannelTransport, function(channel) { chartData = channel.objects.chartData; handleChartChangeEvents(chartData); let count = 0; for(let propertyName in channel.objects) { if (propertyName != 'chartData') { let chartXYData = channel.objects[propertyName]; handleDataChangeEvents(chartXYData); let xDataTemp = chartXYData.m_XData; let yDataTemp = chartXYData.m_YData; let xErrorsTempPlus = chartXYData.m_XErrorDataPlus; let xErrorsTempMinus = chartXYData.m_XErrorDataMinus; let yErrorsTempPlus = chartXYData.m_YErrorDataPlus; let yErrorsTempMinus = chartXYData.m_YErrorDataMinus; let dataLabel = chartXYData.m_Label; dataLabels.push(dataLabel); console.log("loading datalabel: "+dataLabel); //add label to x array xDataTemp.unshift('x'+count.toString()) xs[dataLabel] = 'x' + count.toString() xDataTemp.push(null); //append null value, to make sure the last tick on x-axis is displayed correctly yDataTemp.unshift(dataLabel) yDataTemp.push(null); //append null value, to make sure the last tick on y-axis is displayed correctly xValues[count] = xDataTemp yValues[count] = yDataTemp xErrorValuesPlus[count] = xErrorsTempPlus; xErrorValuesMinus[count] = xErrorsTempMinus; yErrorValuesPlus[count] = yErrorsTempPlus; yErrorValuesMinus[count] = yErrorsTempMinus; var tempLineStyle = ''; if (chartXYData.m_LineStyleName == "solid") { tempLineStyle = '' } else { tempLineStyle = "dashed" } dataProperties[dataLabel] = { "color" : chartXYData.m_Color, "chartType": chartXYData.m_ChartType, "style": tempLineStyle } count++; } } var theme = chartData.m_themeName; setThemeColors(theme); generateChart(chartData); }); } /** * Inits the height of the chart element to 90% of the full window height. */ function initHeight() { var size = window.innerHeight-(window.innerHeight/100*5); //subtract 10% of height to hide vertical scrool bar let chart = document.getElementById("chart"); chart.style.height = `${size}px`; } function getPlotlyChartType(inputType){ let plotlyType = inputType; if (inputType == "line"){ plotlyType = "scatter"; } else if (inputType == "scatter"){ plotlyType = "scatterOnly" } return plotlyType; } /** * Generate error bars object * * @param {array} errors - contains error bar values * @return error bar object */ function generateErrorBars(errors, visible){ let errorObject = { type: 'data', array: errors, visible: visible } return errorObject; } /** * Generate legend position object * * @param legendPosition the string of the legendPosition enum from QmitkChartWidget * @return legend position object */ function generateLegendPosition(legendPosition){ if (legendPosition == "bottomMiddle"){ var legendX = 0.5; var legendY = -0.75; } else if (legendPosition == "bottomRight"){ var legendX = 1; var legendY = 0; } else if (legendPosition == "topRight"){ var legendX = 1; var legendY = 1; } else if (legendPosition == "topLeft"){ var legendX = 0; var legendY = 1; } else if (legendPosition == "middleRight"){ var legendX = 1; var legendY = 0.5; } let legendPositionObject = { x: legendX, y: legendY, font : { color: foregroundColor } } return legendPositionObject; } function generateErrorBarsAsymmetric(errorsPlus, errorsMinus, visible){ let errorObject = generateErrorBars(errorsPlus, visible); errorObject["arrayminus"] = errorsMinus; errorObject["symmetric"] = false; return errorObject; } function generateStackPlotData(){ let data = []; for (let index = 0; index < dataLabels.length; index++){ let inputType = dataProperties[dataLabels[index]]["chartType"]; let chartType = getPlotlyChartType(inputType); let trace = { x: xValues[index].slice(1), y: yValues[index].slice(1), stackgroup: 'one', name: dataLabels[index], type: chartType, marker:{ color: dataProperties[dataLabels[index]]["color"] } }; data.push(trace); } return data; } function generateTraceByChartType(chartType){ let plotlyChartType = getPlotlyChartType(chartType); let trace = { type: plotlyChartType, }; trace["line"] = {} if (plotlyChartType == "area"){ trace["fill"] = 'tozeroy' } else if (plotlyChartType == "spline"){ trace["line"]["shape"] = 'spline' } else if (plotlyChartType == "scatterOnly"){ trace["mode"] = 'markers'; } else if (plotlyChartType == "area-spline"){ trace["fill"] = 'tozeroy' trace["line"]["shape"] = 'spline' } return trace; } function generatePlotData(){ let data = []; for (let index = 0; index < dataLabels.length; index++){ let trace = generateTraceByChartType(dataProperties[dataLabels[index]]["chartType"]); trace["x"] = xValues[index].slice(1); trace["y"] = yValues[index].slice(1); trace["name"] = dataLabels[index]; if(typeof xErrorValuesPlus[index] !== 'undefined'){ if(typeof xErrorValuesMinus[index] !== 'undefined' && xErrorValuesMinus[index].length > 0) { trace["error_x"] = generateErrorBarsAsymmetric(xErrorValuesPlus[index], xErrorValuesMinus[index], chartData.m_ShowErrorBars); }else{ trace["error_x"] = generateErrorBars(xErrorValuesPlus[index], chartData.m_ShowErrorBars); } } if(typeof yErrorValuesPlus[index] !== 'undefined'){ if(typeof yErrorValuesMinus[index] !== 'undefined' && yErrorValuesMinus[index].length > 0) { trace["error_y"] = generateErrorBarsAsymmetric(yErrorValuesPlus[index], yErrorValuesMinus[index], chartData.m_ShowErrorBars); }else{ trace["error_y"] = generateErrorBars(yErrorValuesPlus[index], chartData.m_ShowErrorBars); } } // ===================== CHART TYPE OPTIONS HANDLING =========== trace["line"]["color"] = dataProperties[dataLabels[index]]["color"] // handle marker visibility/size/color trace["marker"] = {size: chartData.m_DataPointSize, color: dataProperties[dataLabels[index]]["color"]} if (chartData.m_DataPointSize == 0){ trace["mode"] = "lines"; } if (dataProperties[dataLabels[index]]["style"] == "dashed"){ trace["line"]["dash"] = "dot" } data.push(trace) } return data; } /** * Here, the chart magic takes place. Plot.ly is called. * * @param {object} chartData - containing the options for plotting, not the actual values */ function generateChart(chartData) { console.log("generate chart"); if (chartData == undefined) { chartData = {} } if (dataLabels == undefined) { dataLabels = [] } //=============================== DATA ======================== var data = []; if (chartData.m_StackedData){ data = generateStackPlotData(); } else { data = generatePlotData(); } //=============================== STYLE ======================== let marginTop = chartData.m_chartTitle == undefined ? 10 : 50; let legendPosition = generateLegendPosition(chartData.m_LegendPosition); var layout = { paper_bgcolor : backgroundColor, plot_bgcolor : backgroundColor, title: { text:chartData.m_chartTitle, font: { color: foregroundColor } }, xaxis: { title: { text: chartData.m_xAxisLabel }, color: foregroundColor }, yaxis: { title: { text:chartData.m_yAxisLabel }, color: foregroundColor }, margin: { l: 50, r: 10, b: 40, t: marginTop, pad: 4 }, showlegend: chartData.m_ShowLegend, legend: legendPosition }; if (chartData.m_StackedData){ layout["barmode"] = 'stack'; } if (chartData.m_YAxisScale){ layout.yaxis["type"] = "log" } if (chartData.m_ShowSubchart){ layout.xaxis.rangeslider = {}; // adds range slider below x axis } Plotly.plot('chart', data, layout, {displayModeBar: false, responsive: true}); } /** * Change theme of chart. * * @param {string} color - dark or not dark */ function changeTheme(color) { setThemeColors(color); link = document.getElementsByTagName("link")[0]; if (color == 'dark') { link.href = "Chart_dark.css"; } else { link.href = "Chart.css"; } }; /** * Reload the chart with the given arguments. * * This method is called by C++. Changes on signature with caution. */ function Reload(){ console.log("Reload chart"); generateChart(chartData); } function SetShowSubchart(showSubchart) { chartData.m_ShowSubchart = showSubchart; } function setThemeColors(theme){ if (theme == 'dark'){ backgroundColor = '#2d2d30'; foregroundColor = 'white'; } else { backgroundColor = '#f0f0f0'; foregroundColor = 'black'; } } function SetStackDataString(stackDataString) { chartData.m_StackedData = stackDataString; } function SetShowErrorBars(showErrorBars) { chartData.m_ShowErrorBars = showErrorBars; } /** * Zooms to the given x-axis min and max values. */ function UpdateMinMaxValueXView(minValueX, maxValueX) { //y-Axis can't be adapted for now. See https://github.com/plotly/plotly.js/issues/1876 let chart = document.getElementById("chart"); let update = { xaxis:{ range:[minValueX, maxValueX] } }; Plotly.relayout(chart, update); } /** * Zooms to the given y-axis min and max values. */ function UpdateMinMaxValueYView(minValueY, maxValueY) { //x-Axis can't be adapted for now. See https://github.com/plotly/plotly.js/issues/1876 let chart = document.getElementById("chart"); let update = { yaxis:{ range:[minValueY, maxValueY] } }; Plotly.relayout(chart, update); } /** * Transforms the view to another chart type. * * This method is called by C++. Changes on signature with caution. * @param {string} transformTo - 'line' or 'bar' */ function transformView(transformTo) { console.log("transform view"); console.log(transformTo); dataProperties[dataLabels[0]]["chartType"] = transformTo; // preserve chartType for later updates let plotlyType = getPlotlyChartType(transformTo); let chart = document.getElementById("chart"); let update = {type : plotlyType}; Plotly.restyle(chart, update, 0); // updates the given plotly trace at index 0 with an update object built of a standard trace object }; diff --git a/Plugins/org.mitk.gui.qt.chartExample/src/internal/ChartExample.cpp b/Plugins/org.mitk.gui.qt.chartExample/src/internal/ChartExample.cpp index 75481dac2c..941ea33a16 100644 --- a/Plugins/org.mitk.gui.qt.chartExample/src/internal/ChartExample.cpp +++ b/Plugins/org.mitk.gui.qt.chartExample/src/internal/ChartExample.cpp @@ -1,350 +1,395 @@ /*=================================================================== The Medical Imaging Interaction Toolkit (MITK) Copyright (c) German Cancer Research Center, Division of Medical and Biological Informatics. All rights reserved. This software is distributed WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See LICENSE.txt or http://www.mitk.org for details. ===================================================================*/ // Blueberry #include <berryIQtStyleManager.h> #include <berryWorkbenchPlugin.h> // Qmitk #include "ChartExample.h" // Qt #include <QRandomGenerator> const std::string ChartExample::VIEW_ID = "org.mitk.views.chartexample"; void ChartExample::SetFocus() { m_Controls.m_buttonCreateChart->setFocus(); } void ChartExample::CreateQtPartControl(QWidget *parent) { // create GUI widgets from the Qt Designer's .ui file m_Controls.setupUi(parent); CreateConnectionsForGUIElements(); m_Controls.m_groupBoxErrors->setVisible(false); m_Controls.m_groupBoxXErrors->setVisible(false); m_Controls.m_groupBoxYErrors->setVisible(false); m_Controls.m_lineEditDataXVector->setVisible(false); m_Controls.m_lineEditDataXVector->setText("0;1;2;3;4;5;6;7;8;9"); m_Controls.m_doubleSpinBox_maxZoomX->setValue(10); m_Controls.m_doubleSpinBox_maxZoomY->setValue(10); FillRandomDataValues(); auto chartStyle = GetColorTheme(); m_Controls.m_Chart->SetTheme(chartStyle); m_Controls.m_lineEditXAxisLabel->setText("xLabel"); m_Controls.m_lineEditYAxisLabel->setText("yLabel"); m_ChartNameToChartType.emplace("bar", QmitkChartWidget::ChartType::bar); m_ChartNameToChartType.emplace("line", QmitkChartWidget::ChartType::line); m_ChartNameToChartType.emplace("spline", QmitkChartWidget::ChartType::spline); m_ChartNameToChartType.emplace("pie", QmitkChartWidget::ChartType::pie); m_ChartNameToChartType.emplace("area", QmitkChartWidget::ChartType::area); m_ChartNameToChartType.emplace("area-spline", QmitkChartWidget::ChartType::area_spline); m_ChartNameToChartType.emplace("scatter", QmitkChartWidget::ChartType::scatter); m_LineNameToLineType.emplace("solid", QmitkChartWidget::LineStyle::solid); m_LineNameToLineType.emplace("dashed", QmitkChartWidget::LineStyle::dashed); m_AxisScaleNameToAxisScaleType.emplace("linear", QmitkChartWidget::AxisScale::linear); m_AxisScaleNameToAxisScaleType.emplace("logarithmic", QmitkChartWidget::AxisScale::log); m_LegendPositionNameToLegendPositionType.emplace("bottom middle", QmitkChartWidget::LegendPosition::bottomMiddle); m_LegendPositionNameToLegendPositionType.emplace("bottom right", QmitkChartWidget::LegendPosition::bottomRight); m_LegendPositionNameToLegendPositionType.emplace("top right", QmitkChartWidget::LegendPosition::topRight); m_LegendPositionNameToLegendPositionType.emplace("top left", QmitkChartWidget::LegendPosition::topLeft); m_LegendPositionNameToLegendPositionType.emplace("middle right", QmitkChartWidget::LegendPosition::middleRight); } void ChartExample::CreateConnectionsForGUIElements() { connect(m_Controls.m_buttonCreateChart, &QPushButton::clicked, this, &ChartExample::CreateChart); connect(m_Controls.m_buttonUpdateChart, &QPushButton::clicked, this, &ChartExample::UpdateChart); connect(m_Controls.m_buttonClearChart, &QPushButton::clicked, this, &ChartExample::ClearChart); connect(m_Controls.m_buttonAddData, &QPushButton::clicked, this, &ChartExample::AddData); connect(m_Controls.m_checkBoxEnableDataX, &QCheckBox::toggled, this, &ChartExample::ShowXData); connect(m_Controls.m_checkBoxEnableErrors, &QCheckBox::toggled, this, &ChartExample::ShowErrorOptions); connect(m_Controls.m_checkBoxEnableXErrors, &QCheckBox::toggled, this, &ChartExample::ShowXErrorOptions); connect(m_Controls.m_checkBoxEnableYErrors, &QCheckBox::toggled, this, &ChartExample::ShowYErrorOptions); connect(m_Controls.m_doubleSpinBox_minZoomX, &QSpinBox::editingFinished, this, &ChartExample::AdaptZoomX); connect(m_Controls.m_doubleSpinBox_maxZoomX, &QSpinBox::editingFinished, this, &ChartExample::AdaptZoomX); connect(m_Controls.m_doubleSpinBox_minZoomY, &QSpinBox::editingFinished, this, &ChartExample::AdaptZoomY); connect(m_Controls.m_doubleSpinBox_maxZoomY, &QSpinBox::editingFinished, this, &ChartExample::AdaptZoomY); connect(m_Controls.m_comboBoxLegendPosition, &QComboBox::currentTextChanged, this, &ChartExample::OnLegendPositionChanged); + connect(m_Controls.m_lineEditTitle, &QLineEdit::editingFinished, this, &ChartExample::OnTitleChanged); + connect(m_Controls.m_lineEditXAxisLabel, &QLineEdit::editingFinished, this, &ChartExample::OnXAxisLabelChanged); + connect(m_Controls.m_lineEditYAxisLabel, &QLineEdit::editingFinished, this, &ChartExample::OnYAxisLabelChanged); + connect( + m_Controls.m_comboBoxYAxisScale, &QComboBox::currentTextChanged, this, &ChartExample::OnYAxisScaleChanged); + connect(m_Controls.m_checkBoxShowLegend, &QCheckBox::stateChanged, this, &ChartExample::OnShowLegendChanged); + connect(m_Controls.m_checkBoxStackedData, &QCheckBox::stateChanged, this, &ChartExample::OnStackedDataChanged); + connect(m_Controls.m_checkBoxShowDataPoints, &QCheckBox::stateChanged, this, &ChartExample::OnShowDataPointsChanged); + connect(m_Controls.m_checkBoxShowSubchart, &QCheckBox::stateChanged, this, &ChartExample::OnShowSubchartChanged); } void ChartExample::FillRandomDataValues() { std::vector<double> numbers = GenerateRandomNumbers(10, 10.0); std::string text = ConvertToText(numbers); m_Controls.m_lineEditDataYVector->setText(QString::fromStdString(text)); m_Controls.m_lineEditDataLabel->setText("test" + QString::number(countForUID)); numbers = GenerateRandomNumbers(10, 10.0); text = ConvertToText(numbers); m_Controls.m_lineEditXErrorPlus->setText(QString::fromStdString(text)); numbers = GenerateRandomNumbers(10, 10.0); text = ConvertToText(numbers); m_Controls.m_lineEditXErrorMinus->setText(QString::fromStdString(text)); numbers = GenerateRandomNumbers(10, 10.0); text = ConvertToText(numbers); m_Controls.m_lineEditYErrorPlus->setText(QString::fromStdString(text)); numbers = GenerateRandomNumbers(10, 10.0); text = ConvertToText(numbers); m_Controls.m_lineEditYErrorMinus->setText(QString::fromStdString(text)); countForUID++; } void ChartExample::CreateChart() { auto dataYAxisScaleType = m_AxisScaleNameToAxisScaleType.at(m_Controls.m_comboBoxYAxisScale->currentText().toStdString()); auto xAxisLabel = m_Controls.m_lineEditXAxisLabel->text().toStdString(); auto yAxisLabel = m_Controls.m_lineEditYAxisLabel->text().toStdString(); auto showLegend = m_Controls.m_checkBoxShowLegend->isChecked(); auto legendPosition = m_LegendPositionNameToLegendPositionType.at(m_Controls.m_comboBoxLegendPosition->currentText().toStdString()); auto showDataPoints = m_Controls.m_checkBoxShowDataPoints->isChecked(); auto stackedData = m_Controls.m_checkBoxStackedData->isChecked(); auto showSubchart = m_Controls.m_checkBoxShowSubchart->isChecked(); - auto title = m_Controls.title->text().toStdString(); + auto title = m_Controls.m_lineEditTitle->text().toStdString(); m_Controls.m_Chart->SetTitle(title); m_Controls.m_Chart->SetYAxisScale(dataYAxisScaleType); m_Controls.m_Chart->SetXAxisLabel(xAxisLabel); m_Controls.m_Chart->SetYAxisLabel(yAxisLabel); m_Controls.m_Chart->SetShowLegend(showLegend); m_Controls.m_Chart->SetLegendPosition(legendPosition); m_Controls.m_Chart->SetShowErrorBars(true); m_Controls.m_Chart->SetShowDataPoints(showDataPoints); m_Controls.m_Chart->SetStackedData(stackedData); m_Controls.m_Chart->Show(showSubchart); } void ChartExample::UpdateChart() { // Test update mechanism m_Controls.m_Chart->SetLineStyle("test0", QmitkChartWidget::LineStyle::dashed); m_Controls.m_Chart->SetChartType("test0", QmitkChartWidget::ChartType::spline); m_Controls.m_Chart->SetColor("test0", "violet"); } void ChartExample::ClearChart() { m_Controls.m_Chart->Clear(); m_Controls.m_plainTextEditDataView->clear(); } std::vector<double> ChartExample::ConvertToVector(const QString &data, QChar delimiter) const { std::vector<double> output; if (data.isEmpty()) { return output; } for (const QString entry : data.split(delimiter)) { output.push_back(entry.toDouble()); } return output; } void ChartExample::AddData() { QString data = m_Controls.m_lineEditDataYVector->text(); auto dataY = ConvertToVector(data); auto chartType = m_ChartNameToChartType.at(m_Controls.m_comboBoxChartType->currentText().toStdString()); std::string dataLabel = m_Controls.m_lineEditDataLabel->text().toStdString(); std::string dataColor = m_Controls.m_lineEditColor->text().toStdString(); auto dataLineStyleType = m_LineNameToLineType.at(m_Controls.m_comboBoxLineStyle->currentText().toStdString()); if (m_Controls.m_checkBoxEnableDataX->isChecked()) { QString lineEditDataX = m_Controls.m_lineEditDataXVector->text(); auto dataX = ConvertToVector(lineEditDataX); if (dataX.size() != dataY.size()) { mitkThrow() << "data x and y size have to be equal"; } auto dataXandY = CreateMap(dataX, dataY); data = QString::fromStdString(ConvertToText(dataXandY)); m_Controls.m_Chart->AddData2D(dataXandY, dataLabel, chartType); } else { m_Controls.m_Chart->AddData1D(dataY, dataLabel, chartType); } if (!dataColor.empty()) { m_Controls.m_Chart->SetColor(dataLabel, dataColor); } if (m_Controls.m_checkBoxEnableErrors->isChecked()) { if (m_Controls.m_checkBoxEnableXErrors->isChecked()) { auto errorsPlus = ConvertToVector(m_Controls.m_lineEditXErrorPlus->text()); auto errorsMinus = ConvertToVector(m_Controls.m_lineEditXErrorMinus->text()); m_Controls.m_Chart->SetXErrorBars(m_Controls.m_lineEditDataLabel->text().toStdString(), errorsPlus, errorsMinus); } if (m_Controls.m_checkBoxEnableYErrors->isChecked()) { auto errorsPlus = ConvertToVector(m_Controls.m_lineEditYErrorPlus->text()); auto errorsMinus = ConvertToVector(m_Controls.m_lineEditYErrorMinus->text()); m_Controls.m_Chart->SetYErrorBars(m_Controls.m_lineEditDataLabel->text().toStdString(), errorsPlus, errorsMinus); } } m_Controls.m_Chart->SetLineStyle(dataLabel, dataLineStyleType); QString dataOverview; dataOverview.append(m_Controls.m_lineEditDataLabel->text()); dataOverview.append("(").append(m_Controls.m_comboBoxChartType->currentText()); if (!dataColor.empty()) { dataOverview.append(", ").append(dataColor.c_str()); } dataOverview.append(", ").append(m_Controls.m_comboBoxLineStyle->currentText()); dataOverview.append(")"); dataOverview.append(":").append(data); m_Controls.m_plainTextEditDataView->appendPlainText(dataOverview); FillRandomDataValues(); } void ChartExample::ShowXData(bool show) { m_Controls.m_lineEditDataXVector->setVisible(show); } void ChartExample::ShowErrorOptions(bool show) { m_Controls.m_groupBoxErrors->setVisible(show); } void ChartExample::ShowXErrorOptions(bool show) { m_Controls.m_groupBoxXErrors->setVisible(show); } void ChartExample::ShowYErrorOptions(bool show) { m_Controls.m_groupBoxYErrors->setVisible(show); } void ChartExample::AdaptZoomX() { m_Controls.m_Chart->UpdateMinMaxValueXView(m_Controls.m_doubleSpinBox_minZoomX->value(), m_Controls.m_doubleSpinBox_maxZoomX->value()); } void ChartExample::AdaptZoomY() { m_Controls.m_Chart->UpdateMinMaxValueYView(m_Controls.m_doubleSpinBox_minZoomY->value(), m_Controls.m_doubleSpinBox_maxZoomY->value()); } std::vector<double> ChartExample::GenerateRandomNumbers(unsigned int amount, double max) const { QRandomGenerator gen; gen.seed(time(NULL)); std::vector<double> data; for (unsigned int i = 0; i < amount; i++) { data.push_back(gen.bounded(max)); } return data; } std::map<double, double> ChartExample::CreateMap(std::vector<double> keys, std::vector<double> values) const { std::map<double, double> aMap; std::transform(keys.begin(), keys.end(), values.begin(), std::inserter(aMap, aMap.end()), [](double a, double b) { return std::make_pair(a, b); }); return aMap; } std::string ChartExample::ConvertToText(std::vector<double> numbers, std::string delimiter) const { std::ostringstream oss; oss.precision(3); if (!numbers.empty()) { for (auto number : numbers) { oss << number << delimiter; } } auto aString = oss.str(); aString.pop_back(); return aString; } std::string ChartExample::ConvertToText(std::map<double, double> numbers, std::string delimiter) const { std::ostringstream oss; oss.precision(3); if (!numbers.empty()) { for (const auto keyValue : numbers) { oss << keyValue.first << ":" << keyValue.second << delimiter; } } auto aString = oss.str(); aString.pop_back(); return aString; } QmitkChartWidget::ColorTheme ChartExample::GetColorTheme() const { ctkPluginContext *context = berry::WorkbenchPlugin::GetDefault()->GetPluginContext(); ctkServiceReference styleManagerRef = context->getServiceReference<berry::IQtStyleManager>(); if (styleManagerRef) { auto styleManager = context->getService<berry::IQtStyleManager>(styleManagerRef); if (styleManager->GetStyle().name == "Dark") { return QmitkChartWidget::ColorTheme::darkstyle; } else { return QmitkChartWidget::ColorTheme::lightstyle; } } return QmitkChartWidget::ColorTheme::darkstyle; } -void ChartExample::OnLegendPositionChanged() { - auto legendPosition = - m_LegendPositionNameToLegendPositionType.at(m_Controls.m_comboBoxLegendPosition->currentText().toStdString()); +void ChartExample::OnLegendPositionChanged(const QString &newText) +{ + auto legendPosition = m_LegendPositionNameToLegendPositionType.at(newText.toStdString()); m_Controls.m_Chart->SetLegendPosition(legendPosition); } + +void ChartExample::OnTitleChanged() { + auto newTitle = m_Controls.m_lineEditTitle->text(); + m_Controls.m_Chart->SetTitle(newTitle.toStdString()); +} + +void ChartExample::OnXAxisLabelChanged() { + auto newXAxisLabel = m_Controls.m_lineEditXAxisLabel->text(); + m_Controls.m_Chart->SetXAxisLabel(newXAxisLabel.toStdString()); +} + +void ChartExample::OnYAxisLabelChanged() { + auto newYAxisLabel = m_Controls.m_lineEditYAxisLabel->text(); + m_Controls.m_Chart->SetYAxisLabel(newYAxisLabel.toStdString()); +} + +void ChartExample::OnYAxisScaleChanged(const QString &newYAxisScale) { + auto yAxisScale = m_AxisScaleNameToAxisScaleType.at(newYAxisScale.toStdString()); + m_Controls.m_Chart->SetYAxisScale(yAxisScale); +} + +void ChartExample::OnShowLegendChanged(int newState) { + m_Controls.m_Chart->SetShowLegend(newState == Qt::Checked); +} + +void ChartExample::OnStackedDataChanged(int newState) { + m_Controls.m_Chart->SetStackedData(newState == Qt::Checked); +} + +void ChartExample::OnShowDataPointsChanged(int newState) { + m_Controls.m_Chart->SetShowDataPoints(newState == Qt::Checked); +} + +void ChartExample::OnShowSubchartChanged(int newState) { + m_Controls.m_Chart->SetShowSubchart(newState == Qt::Checked); +} diff --git a/Plugins/org.mitk.gui.qt.chartExample/src/internal/ChartExample.h b/Plugins/org.mitk.gui.qt.chartExample/src/internal/ChartExample.h index e35a7253eb..4a78de4016 100644 --- a/Plugins/org.mitk.gui.qt.chartExample/src/internal/ChartExample.h +++ b/Plugins/org.mitk.gui.qt.chartExample/src/internal/ChartExample.h @@ -1,79 +1,87 @@ /*=================================================================== The Medical Imaging Interaction Toolkit (MITK) Copyright (c) German Cancer Research Center, Division of Medical and Biological Informatics. All rights reserved. This software is distributed WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See LICENSE.txt or http://www.mitk.org for details. ===================================================================*/ #ifndef ChartExample_h #define ChartExample_h #include <QmitkAbstractView.h> #include "ui_ChartExampleControls.h" /** \brief Basic example for use of module mitkChart \sa QmitkAbstractView \ingroup ${plugin_target}_internal */ class ChartExample : public QmitkAbstractView { // this is needed for all Qt objects that should have a Qt meta-object // (everything that derives from QObject and wants to have signal/slots) Q_OBJECT public: static const std::string VIEW_ID; protected: virtual void CreateQtPartControl(QWidget *parent) override; void CreateConnectionsForGUIElements(); virtual void SetFocus() override; void CreateChart(); void UpdateChart(); void ClearChart(); void AddData(); void ShowXData(bool show); void ShowErrorOptions(bool show); void ShowXErrorOptions(bool show); void ShowYErrorOptions(bool show); void AdaptZoomX(); void AdaptZoomY(); private: void FillRandomDataValues(); std::vector<double> GenerateRandomNumbers(unsigned int amount, double max) const; std::vector<double> ConvertToVector(const QString &data, QChar delimiter = ';') const; std::map<double, double> CreateMap(std::vector<double> keys, std::vector<double> values) const; std::string ConvertToText(std::vector<double> numbers, std::string delimiter = ";") const; std::string ConvertToText(std::map<double, double> numbers, std::string delimiter = ";") const; QmitkChartWidget::ColorTheme GetColorTheme() const; - void OnLegendPositionChanged(); + void OnLegendPositionChanged(const QString &newPosition); + void OnTitleChanged(); + void OnXAxisLabelChanged(); + void OnYAxisLabelChanged(); + void OnYAxisScaleChanged(const QString &newYAxisScale); + void OnShowLegendChanged(int newState); + void OnStackedDataChanged(int newState); + void OnShowDataPointsChanged(int newState); + void OnShowSubchartChanged(int newState); std::map<std::string, QmitkChartWidget::ChartType> m_ChartNameToChartType; std::map<std::string, QmitkChartWidget::LineStyle> m_LineNameToLineType; std::map<std::string, QmitkChartWidget::AxisScale> m_AxisScaleNameToAxisScaleType; std::map<std::string, QmitkChartWidget::LegendPosition> m_LegendPositionNameToLegendPositionType; unsigned int countForUID = 0; Ui::ChartExampleControls m_Controls; }; #endif // ChartExample_h diff --git a/Plugins/org.mitk.gui.qt.chartExample/src/internal/ChartExampleControls.ui b/Plugins/org.mitk.gui.qt.chartExample/src/internal/ChartExampleControls.ui index 7c441cb30f..3ef75a0a60 100644 --- a/Plugins/org.mitk.gui.qt.chartExample/src/internal/ChartExampleControls.ui +++ b/Plugins/org.mitk.gui.qt.chartExample/src/internal/ChartExampleControls.ui @@ -1,666 +1,666 @@ <?xml version="1.0" encoding="UTF-8"?> <ui version="4.0"> <class>ChartExampleControls</class> <widget class="QWidget" name="ChartExampleControls"> <property name="geometry"> <rect> <x>0</x> <y>0</y> <width>455</width> <height>1054</height> </rect> </property> <property name="minimumSize"> <size> <width>0</width> <height>0</height> </size> </property> <property name="windowTitle"> <string>QmitkTemplate</string> </property> <layout class="QVBoxLayout" name="verticalLayout"> <item> <widget class="QToolBox" name="toolBox"> <property name="sizePolicy"> <sizepolicy hsizetype="Preferred" vsizetype="Expanding"> <horstretch>0</horstretch> <verstretch>0</verstretch> </sizepolicy> </property> <property name="currentIndex"> <number>0</number> </property> <widget class="QWidget" name="page"> <property name="geometry"> <rect> <x>0</x> <y>0</y> <width>420</width> <height>411</height> </rect> </property> <property name="sizePolicy"> <sizepolicy hsizetype="Preferred" vsizetype="Expanding"> <horstretch>0</horstretch> <verstretch>0</verstretch> </sizepolicy> </property> <attribute name="label"> <string>Data</string> </attribute> <layout class="QVBoxLayout" name="verticalLayout_7"> <item> <layout class="QFormLayout" name="formLayout"> <item row="0" column="0"> <widget class="QLabel" name="label"> <property name="text"> <string>data entry</string> </property> </widget> </item> <item row="0" column="1"> <layout class="QFormLayout" name="formLayout_6"> <item row="1" column="1"> <widget class="QLineEdit" name="m_lineEditDataYVector"/> </item> <item row="0" column="1"> <widget class="QLineEdit" name="m_lineEditDataXVector"> <property name="sizePolicy"> <sizepolicy hsizetype="Expanding" vsizetype="Preferred"> <horstretch>0</horstretch> <verstretch>0</verstretch> </sizepolicy> </property> <property name="text"> <string/> </property> </widget> </item> <item row="0" column="0"> <widget class="QCheckBox" name="m_checkBoxEnableDataX"> <property name="text"> <string>x</string> </property> </widget> </item> <item row="1" column="0"> <widget class="QLabel" name="label_13"> <property name="text"> <string>y:</string> </property> </widget> </item> </layout> </item> <item row="1" column="0"> <widget class="QCheckBox" name="m_checkBoxEnableErrors"> <property name="text"> <string>error</string> </property> </widget> </item> <item row="1" column="1"> <widget class="QGroupBox" name="m_groupBoxErrors"> <property name="minimumSize"> <size> <width>0</width> <height>0</height> </size> </property> <property name="title"> <string>Error values</string> </property> <layout class="QVBoxLayout" name="verticalLayout_4"> <item> <layout class="QFormLayout" name="formLayout_3"> <item row="0" column="0"> <widget class="QCheckBox" name="m_checkBoxEnableYErrors"> <property name="text"> <string>y error</string> </property> </widget> </item> <item row="2" column="0"> <widget class="QCheckBox" name="m_checkBoxEnableXErrors"> <property name="text"> <string>x error</string> </property> </widget> </item> <item row="2" column="1"> <widget class="QGroupBox" name="m_groupBoxXErrors"> <property name="minimumSize"> <size> <width>0</width> <height>0</height> </size> </property> <property name="title"> <string>GroupBox</string> </property> <layout class="QVBoxLayout" name="verticalLayout_6"> <item> <layout class="QFormLayout" name="formLayout_5"> <item row="0" column="0"> <widget class="QLabel" name="label_11"> <property name="text"> <string>plus error</string> </property> </widget> </item> <item row="1" column="0"> <widget class="QLabel" name="label_12"> <property name="text"> <string>minus error</string> </property> </widget> </item> <item row="0" column="1"> <widget class="QLineEdit" name="m_lineEditXErrorPlus"/> </item> <item row="1" column="1"> <widget class="QLineEdit" name="m_lineEditXErrorMinus"/> </item> </layout> </item> </layout> </widget> </item> <item row="0" column="1"> <widget class="QGroupBox" name="m_groupBoxYErrors"> <property name="minimumSize"> <size> <width>0</width> <height>0</height> </size> </property> <property name="title"> <string>GroupBox</string> </property> <layout class="QVBoxLayout" name="verticalLayout_5"> <item> <layout class="QFormLayout" name="formLayout_4"> <item row="0" column="0"> <widget class="QLabel" name="label_10"> <property name="text"> <string>plus error</string> </property> </widget> </item> <item row="0" column="1"> <widget class="QLineEdit" name="m_lineEditYErrorPlus"> <property name="sizePolicy"> <sizepolicy hsizetype="Expanding" vsizetype="Preferred"> <horstretch>0</horstretch> <verstretch>0</verstretch> </sizepolicy> </property> <property name="text"> <string/> </property> </widget> </item> <item row="1" column="1"> <widget class="QLineEdit" name="m_lineEditYErrorMinus"> <property name="sizePolicy"> <sizepolicy hsizetype="Expanding" vsizetype="Preferred"> <horstretch>0</horstretch> <verstretch>0</verstretch> </sizepolicy> </property> <property name="text"> <string/> </property> </widget> </item> <item row="1" column="0"> <widget class="QLabel" name="label_6"> <property name="text"> <string>minus error</string> </property> </widget> </item> </layout> </item> </layout> </widget> </item> </layout> </item> </layout> </widget> </item> <item row="2" column="0"> <widget class="QLabel" name="label_5"> <property name="text"> <string>data label</string> </property> </widget> </item> <item row="2" column="1"> <widget class="QLineEdit" name="m_lineEditDataLabel"> <property name="sizePolicy"> <sizepolicy hsizetype="Expanding" vsizetype="Preferred"> <horstretch>0</horstretch> <verstretch>0</verstretch> </sizepolicy> </property> </widget> </item> <item row="3" column="0"> <widget class="QLabel" name="label_2"> <property name="text"> <string>Chart type</string> </property> </widget> </item> <item row="3" column="1"> <widget class="QComboBox" name="m_comboBoxChartType"> <property name="sizePolicy"> <sizepolicy hsizetype="Preferred" vsizetype="Preferred"> <horstretch>0</horstretch> <verstretch>0</verstretch> </sizepolicy> </property> <item> <property name="text"> <string>bar</string> </property> </item> <item> <property name="text"> <string>line</string> </property> </item> <item> <property name="text"> <string>area</string> </property> </item> <item> <property name="text"> <string>spline</string> </property> </item> <item> <property name="text"> <string>area-spline</string> </property> </item> <item> <property name="text"> <string>scatter</string> </property> </item> </widget> </item> <item row="4" column="0"> <widget class="QLabel" name="label_7"> <property name="text"> <string>Color</string> </property> </widget> </item> <item row="4" column="1"> <widget class="QLineEdit" name="m_lineEditColor"> <property name="sizePolicy"> <sizepolicy hsizetype="Expanding" vsizetype="Preferred"> <horstretch>0</horstretch> <verstretch>0</verstretch> </sizepolicy> </property> </widget> </item> <item row="5" column="0"> <widget class="QLabel" name="label_8"> <property name="text"> <string>Line style</string> </property> </widget> </item> <item row="5" column="1"> <widget class="QComboBox" name="m_comboBoxLineStyle"> <property name="sizePolicy"> <sizepolicy hsizetype="Preferred" vsizetype="Preferred"> <horstretch>0</horstretch> <verstretch>0</verstretch> </sizepolicy> </property> <item> <property name="text"> <string>solid</string> </property> </item> <item> <property name="text"> <string>dashed</string> </property> </item> </widget> </item> </layout> </item> <item> <widget class="QPushButton" name="m_buttonAddData"> <property name="text"> <string>Add data</string> </property> </widget> </item> </layout> </widget> <widget class="QWidget" name="page_2"> <property name="geometry"> <rect> <x>0</x> <y>0</y> - <width>225</width> + <width>437</width> <height>236</height> </rect> </property> <property name="sizePolicy"> <sizepolicy hsizetype="Preferred" vsizetype="Expanding"> <horstretch>0</horstretch> <verstretch>0</verstretch> </sizepolicy> </property> <attribute name="label"> <string>Global options</string> </attribute> <layout class="QVBoxLayout" name="verticalLayout_8"> <item> <layout class="QFormLayout" name="formLayout_2"> <property name="sizeConstraint"> <enum>QLayout::SetDefaultConstraint</enum> </property> <property name="fieldGrowthPolicy"> <enum>QFormLayout::AllNonFixedFieldsGrow</enum> </property> <item row="0" column="0"> <widget class="QLabel" name="titleLabel"> <property name="text"> <string>Title</string> </property> </widget> </item> <item row="0" column="1"> - <widget class="QLineEdit" name="title"/> + <widget class="QLineEdit" name="m_lineEditTitle"/> </item> <item row="1" column="0"> <widget class="QLabel" name="label_3"> <property name="text"> <string>XAxis label</string> </property> </widget> </item> <item row="1" column="1"> <widget class="QLineEdit" name="m_lineEditXAxisLabel"/> </item> <item row="2" column="0"> <widget class="QLabel" name="label_4"> <property name="text"> <string>YAxis label</string> </property> </widget> </item> <item row="2" column="1"> <widget class="QLineEdit" name="m_lineEditYAxisLabel"/> </item> <item row="3" column="0"> <widget class="QLabel" name="label_9"> <property name="text"> <string>Y Axis scale</string> </property> </widget> </item> <item row="3" column="1"> <widget class="QComboBox" name="m_comboBoxYAxisScale"> <item> <property name="text"> <string>linear</string> </property> </item> <item> <property name="text"> <string>logarithmic</string> </property> </item> </widget> </item> <item row="5" column="0"> <widget class="QCheckBox" name="m_checkBoxShowLegend"> <property name="text"> <string>Show legend</string> </property> <property name="checked"> <bool>true</bool> </property> </widget> </item> <item row="6" column="0"> <widget class="QCheckBox" name="m_checkBoxStackedData"> <property name="text"> <string>Stacked data</string> </property> </widget> </item> <item row="7" column="0"> <widget class="QCheckBox" name="m_checkBoxShowDataPoints"> <property name="text"> <string>Show data points</string> </property> <property name="checked"> <bool>true</bool> </property> </widget> </item> <item row="8" column="0"> <widget class="QCheckBox" name="m_checkBoxShowSubchart"> <property name="text"> <string>Show Subchart</string> </property> </widget> </item> <item row="4" column="0"> <widget class="QLabel" name="label_16"> <property name="text"> <string>Legend position</string> </property> </widget> </item> <item row="4" column="1"> <widget class="QComboBox" name="m_comboBoxLegendPosition"> <property name="currentIndex"> <number>2</number> </property> <item> <property name="text"> <string>bottom middle</string> </property> </item> <item> <property name="text"> <string>bottom right</string> </property> </item> <item> <property name="text"> <string>top right</string> </property> </item> <item> <property name="text"> <string>top left</string> </property> </item> <item> <property name="text"> <string>middle right</string> </property> </item> </widget> </item> </layout> </item> </layout> </widget> <widget class="QWidget" name="page_3"> <property name="geometry"> <rect> <x>0</x> <y>0</y> - <width>100</width> - <height>30</height> + <width>437</width> + <height>177</height> </rect> </property> <attribute name="label"> <string>Zoom</string> </attribute> <widget class="QWidget" name="gridLayoutWidget"> <property name="geometry"> <rect> <x>0</x> <y>0</y> <width>431</width> <height>121</height> </rect> </property> <layout class="QGridLayout" name="gridLayout"> <item row="0" column="0"> <widget class="QLabel" name="label_14"> <property name="text"> <string>x:</string> </property> </widget> </item> <item row="1" column="0"> <widget class="QLabel" name="label_15"> <property name="text"> <string>y:</string> </property> </widget> </item> <item row="0" column="1"> <widget class="QDoubleSpinBox" name="m_doubleSpinBox_minZoomX"/> </item> <item row="1" column="1"> <widget class="QDoubleSpinBox" name="m_doubleSpinBox_minZoomY"/> </item> <item row="0" column="2"> <widget class="QDoubleSpinBox" name="m_doubleSpinBox_maxZoomX"/> </item> <item row="1" column="2"> <widget class="QDoubleSpinBox" name="m_doubleSpinBox_maxZoomY"/> </item> </layout> </widget> </widget> </widget> </item> <item> <widget class="QPushButton" name="m_buttonCreateChart"> <property name="toolTip"> <string>Do image processing</string> </property> <property name="text"> <string>Create chart</string> </property> </widget> </item> <item> <widget class="QPushButton" name="m_buttonUpdateChart"> <property name="toolTip"> <string>Do image processing</string> </property> <property name="text"> <string>Update chart</string> </property> </widget> </item> <item> <widget class="QPushButton" name="m_buttonClearChart"> <property name="text"> <string>Clear chart</string> </property> </widget> </item> <item> <widget class="Line" name="line"> <property name="orientation"> <enum>Qt::Vertical</enum> </property> </widget> </item> <item> <widget class="QStackedWidget" name="m_StatisticsWidgetStack"> <property name="sizePolicy"> <sizepolicy hsizetype="Expanding" vsizetype="Expanding"> <horstretch>0</horstretch> <verstretch>0</verstretch> </sizepolicy> </property> <property name="minimumSize"> <size> <width>0</width> <height>250</height> </size> </property> <property name="currentIndex"> <number>0</number> </property> <widget class="QmitkChartWidget" name="m_Chart"> <property name="sizePolicy"> <sizepolicy hsizetype="Preferred" vsizetype="Fixed"> <horstretch>0</horstretch> <verstretch>0</verstretch> </sizepolicy> </property> </widget> </widget> </item> <item> <widget class="QPlainTextEdit" name="m_plainTextEditDataView"> <property name="sizePolicy"> <sizepolicy hsizetype="Preferred" vsizetype="Preferred"> <horstretch>0</horstretch> <verstretch>0</verstretch> </sizepolicy> </property> <property name="maximumSize"> <size> <width>16777215</width> <height>150</height> </size> </property> </widget> </item> <item> <spacer name="spacer1"> <property name="orientation"> <enum>Qt::Vertical</enum> </property> <property name="sizeType"> <enum>QSizePolicy::Expanding</enum> </property> <property name="sizeHint" stdset="0"> <size> <width>20</width> <height>0</height> </size> </property> </spacer> </item> </layout> </widget> <layoutdefault spacing="6" margin="11"/> <customwidgets> <customwidget> <class>QmitkChartWidget</class> <extends>QWidget</extends> <header location="global">QmitkChartWidget.h</header> </customwidget> </customwidgets> <resources/> <connections/> </ui>