Skip to content

Commit

Permalink
Fix marker opacity of mostly null data series (#1941)
Browse files Browse the repository at this point in the history
* Fix marker opacity when most of the data is null

* Add tracking of non-null values for aggregate plots
  • Loading branch information
aestoltm authored Dec 3, 2024
1 parent b29a5ac commit bfcd67e
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 28 deletions.
20 changes: 16 additions & 4 deletions classes/DataWarehouse/Visualization/AggregateChart.php
Original file line number Diff line number Diff line change
Expand Up @@ -949,6 +949,7 @@ public function configure(
$drillable = array();
$text = array();
$colors = array();
$visiblePoints = 0;

// to display as pie chart:
if($data_description->display_type == 'pie')
Expand Down Expand Up @@ -976,6 +977,9 @@ public function configure(
'values' => $yValues,
);

if (!is_null($value)) {
$visiblePoints++;
}
}
// Dont add data labels for all pie slices. Plotly will render all labels otherwise,
// which causes the margin on pie charts with many slices to break
Expand Down Expand Up @@ -1016,6 +1020,10 @@ public function configure(
'id' => $yAxisDataObject->getXId($index),
'label' => $yAxisDataObject->getXValue($index)
);

if (!is_null($value)) {
$visiblePoints++;
}
}
}

Expand Down Expand Up @@ -1084,10 +1092,14 @@ public function configure(
}

$this->_chart['layout']['hoverlabel']['bordercolor'] = $yAxisColor;
// Hide markers for 32 points or greater, except when there are multiple traces then hide markers starting at 21 points.

// Show markers if the non-thumbnail plot has less than 21 visible data points.
// Also show markers if there is one data point otherwise thumbnail plots with 1 non-null point will be
// hidden.
// Need check for chart types that this applies to otherwise bar, scatter, and pie charts will be hidden.
$hideMarker = in_array($data_description->display_type, array('line', 'spline', 'area', 'areaspline'))
&& ($values_count >= 32 || (count($yAxisObject->series) > 1 && $values_count >= 21));
$showMarker = in_array($data_description->display_type, array('scatter', 'pie', 'bar', 'h_bar', 'column'))
|| ($visiblePoints < 21 && !$isThumbnail)
|| $visiblePoints == 1;

$trace = array_merge($trace, array(
'automargin'=> $data_description->display_type == 'pie' ? true : null,
Expand All @@ -1107,7 +1119,7 @@ public function configure(
'color' => $lineColor,
),
'symbol' => $this->_symbolStyles[$data_description_index % 5],
'opacity' => $hideMarker ? 0.0 : 1.0
'opacity' => $showMarker ? 1.0 : 0.0
),
'line' => array(
'color' => $data_description->display_type == 'pie' ?
Expand Down
39 changes: 15 additions & 24 deletions classes/DataWarehouse/Visualization/TimeseriesChart.php
Original file line number Diff line number Diff line change
Expand Up @@ -89,6 +89,7 @@ public function configure(


$this->show_filters = $show_filters;
$isThumbnail = $this->_width <= \DataWarehouse\Visualization::$thumbnail_width;

// Instantiate the color generator:
$colorGenerator = new \DataWarehouse\Visualization\ColorGenerator();
Expand Down Expand Up @@ -435,40 +436,31 @@ public function configure(
$color = '#'.str_pad(dechex($color_value), 6, '0', STR_PAD_LEFT);
$lineColor = '#'.str_pad(dechex(\DataWarehouse\Visualization::alterBrightness($color_value, -70)), 6, '0', STR_PAD_LEFT);

//chart chokes on datasets that are all null so detect them and replace
// all with zero. this will give the user the right impression. (hopefully)
$all_null = true;
// Calculate number of non-null values to determine data series marker visibility.
// If all values are null then skip this series.
$allNull = true;
$visiblePoints = 0;
foreach($yAxisDataObject->getValues() as $value)
{
if($value != null)
{
$all_null = false;
break;
$allNull = false;
$visiblePoints++;
}
}
if($all_null)
if($allNull)
{
continue;
}
$values = $yAxisDataObject->getValues();

// Decide whether to show data point markers:
$values_count = count($values);
// Count datapoints having actual, non-null y values:
$y_values_count = 0;
foreach ($values as $y_value) {
if ($y_value !== null) {
++$y_values_count;
}
// we are only interested in the == 1 case.
if ($y_values_count > 1) {
break;
}
}
// Hide markers for 32 points or greater, except when there are multiple traces then hide markers starting at 21 points.
// Show markers if the non-thumbnail plot has less than 21 visible data points.
// Also show markers if there is one data point otherwise thumbnail plots with 1 non-null point will be
// hidden.
// Need check for chart types that this applies to otherwise bar, scatter, and pie charts will be hidden.
$hideMarker = in_array($data_description->display_type, array('line', 'spline', 'area', 'areaspline'))
&& ($values_count >= 32 || (count($yAxisDataObjectsArray) > 1 && $values_count >= 21));
$showMarker = in_array($data_description->display_type, array('scatter', 'pie', 'bar', 'h_bar', 'column'))
|| ($visiblePoints < 21 && !$isThumbnail)
|| $visiblePoints == 1;

$isRemainder = $yAxisDataObject->getGroupId() === TimeseriesDataset::SUMMARY_GROUP_ID;

Expand Down Expand Up @@ -588,7 +580,7 @@ public function configure(
'color' => $lineColor
),
'symbol' => $this->_symbolStyles[$traceIndex % 5],
'opacity' => $hideMarker ? 0.0 : 1.0
'opacity' => $showMarker ? 1.0 : 0.0
),
'type' => $data_description->display_type == 'h_bar' || $data_description->display_type == 'column' ? 'bar' : $data_description->display_type,
'line' => array(
Expand Down Expand Up @@ -641,7 +633,6 @@ public function configure(
// Set date tick interval
$this->_chart['layout']['xaxis']['dtick'] = $pointInterval;
$this->_chart['layout']['xaxis']['tick0'] = $xValues[0];
$isThumbnail = $this->_width <= \DataWarehouse\Visualization::$thumbnail_width;
$value_count = count($xValues);

if (($this->_aggregationUnit == 'Day' || $this->_aggregationUnit == 'day')) {
Expand Down

0 comments on commit bfcd67e

Please sign in to comment.