Skip to content
This repository has been archived by the owner on Jan 9, 2023. It is now read-only.

Commit

Permalink
GitHub Sync (#21)
Browse files Browse the repository at this point in the history
* Time series charts with one datum will show a circle.

PiperOrigin-RevId: 189978850

* Internal changes.

PiperOrigin-RevId: 190084978

* Make charts_flutter types generic to support Dart 2

PiperOrigin-RevId: 190093435

* Update test color

PiperOrigin-RevId: 190099422

*  Add generic types to several classes to enable Dart 2 static typing

PiperOrigin-RevId: 190134794

* Added animation to pie charts.

PiperOrigin-RevId: 190229468

* Another Dart 2 fix

PiperOrigin-RevId: 190229795
  • Loading branch information
nshahan committed Mar 23, 2018
1 parent 1d4d885 commit be1bc84
Show file tree
Hide file tree
Showing 30 changed files with 348 additions and 126 deletions.
3 changes: 3 additions & 0 deletions charts_common/lib/common.dart
Expand Up @@ -27,6 +27,8 @@ export 'src/chart/common/base_chart.dart' show BaseChart, LifecycleListener;
export 'src/chart/common/canvas_shapes.dart'
show CanvasBarStack, CanvasPie, CanvasPieSlice, CanvasRect;
export 'src/chart/common/chart_context.dart' show ChartContext;
export 'src/chart/common/datum_details.dart'
show DatumDetails, DomainFormatter, MeasureFormatter;
export 'src/chart/common/series_renderer.dart' show SeriesRenderer;
export 'src/chart/common/series_renderer_config.dart'
show RendererAttributeKey, SeriesRendererConfig;
Expand Down Expand Up @@ -75,6 +77,7 @@ export 'src/chart/bar/bar_renderer_decorator.dart' show BarRendererDecorator;
export 'src/chart/bar/bar_label_decorator.dart' show BarLabelDecorator;
export 'src/chart/bar/base_bar_renderer_config.dart'
show BarGroupingType, BaseBarRendererConfig;
export 'src/chart/cartesian/axis/axis.dart' show domainAxisKey, measureAxisKey;
export 'src/chart/cartesian/axis/spec/axis_spec.dart' show AxisSpec;
export 'src/chart/cartesian/cartesian_chart.dart' show CartesianChart;
export 'src/chart/cartesian/cartesian_renderer.dart' show BaseCartesianRenderer;
Expand Down
6 changes: 3 additions & 3 deletions charts_common/lib/src/chart/bar/bar_label_decorator.dart
Expand Up @@ -24,7 +24,7 @@ import '../../common/text_style.dart' show TextStyle;
import 'bar_renderer.dart' show ImmutableBarRendererElement;
import 'bar_renderer_decorator.dart' show BarRendererDecorator;

class BarLabelDecorator extends BarRendererDecorator {
class BarLabelDecorator<T, D> extends BarRendererDecorator<T, D> {
// Default configuration
static const _defaultLabelPosition = BarLabelPosition.auto;
static const _defaultLabelPadding = 5;
Expand Down Expand Up @@ -60,7 +60,7 @@ class BarLabelDecorator extends BarRendererDecorator {
outsideLabelStyleSpec ?? _defaultOutsideLabelStyle;

@override
void decorate(Iterable<ImmutableBarRendererElement> barElements,
void decorate(Iterable<ImmutableBarRendererElement<T, D>> barElements,
ChartCanvas canvas, GraphicsFactory graphicsFactory,
{@required Rectangle drawBounds,
@required double animationPercent,
Expand All @@ -81,7 +81,7 @@ class BarLabelDecorator extends BarRendererDecorator {
final outsideLabelStyle =
_getTextStyle(graphicsFactory, outsideLabelStyleSpec);

for (ImmutableBarRendererElement element in barElements) {
for (var element in barElements) {
final labelFn = element.series.labelAccessorFn;
final label = (labelFn != null) ? labelFn(element.datum, null) : null;

Expand Down
37 changes: 19 additions & 18 deletions charts_common/lib/src/chart/bar/bar_renderer.dart
Expand Up @@ -29,8 +29,8 @@ import '../common/processed_series.dart' show ImmutableSeries, MutableSeries;
import '../../common/color.dart' show Color;

/// Renders series data as a series of bars.
class BarRenderer<T, D>
extends BaseBarRenderer<T, D, _BarRendererElement, _AnimatedBar<T, D>> {
class BarRenderer<T, D> extends BaseBarRenderer<T, D, _BarRendererElement<T, D>,
_AnimatedBar<T, D>> {
/// If we are grouped, use this spacing between the bars in a group.
final _barGroupInnerPadding = 2;

Expand Down Expand Up @@ -65,8 +65,8 @@ class BarRenderer<T, D>
}

@override
_BarRendererElement getBaseDetails(T datum, int index) {
return new _BarRendererElement()..roundPx = _roundingRadiusPx;
_BarRendererElement<T, D> getBaseDetails(T datum, int index) {
return new _BarRendererElement<T, D>()..roundPx = _roundingRadiusPx;
}

bool get rtl => _chart.context.rtl;
Expand All @@ -79,7 +79,7 @@ class BarRenderer<T, D>
ImmutableSeries<T, D> series,
T datum,
Color color,
_BarRendererElement details,
_BarRendererElement<T, D> details,
D domainValue,
ImmutableAxis<D> domainAxis,
int domainWidth,
Expand Down Expand Up @@ -112,9 +112,9 @@ class BarRenderer<T, D>
/// Generates a [_BarRendererElement] to represent the rendering data for one
/// bar on the chart.
@override
_BarRendererElement makeBarRendererElement(
_BarRendererElement<T, D> makeBarRendererElement(
{Color color,
_BarRendererElement details,
_BarRendererElement<T, D> details,
D domainValue,
ImmutableAxis<D> domainAxis,
int domainWidth,
Expand All @@ -126,7 +126,7 @@ class BarRenderer<T, D>
double strokeWidthPx,
int barGroupIndex,
int numBarGroups}) {
return new _BarRendererElement()
return new _BarRendererElement<T, D>()
..color = color
..roundPx = details.roundPx
..measureAxisPosition = measureAxisPosition
Expand Down Expand Up @@ -154,7 +154,7 @@ class BarRenderer<T, D>

@override
void paintBar(ChartCanvas canvas, double animationPercent,
Iterable<_BarRendererElement> barElements) {
Iterable<_BarRendererElement<T, D>> barElements) {
final bars = <CanvasRect>[];

// When adjusting bars for stacked bar padding, do not modify the first bar
Expand All @@ -163,7 +163,7 @@ class BarRenderer<T, D>
final unmodifiedBar =
renderingVertically ? barElements.first : barElements.last;

for (_BarRendererElement bar in barElements) {
for (var bar in barElements) {
var bounds = bar.bounds;

if (bar != unmodifiedBar) {
Expand Down Expand Up @@ -260,14 +260,14 @@ class BarRenderer<T, D>

abstract class ImmutableBarRendererElement<T, D> {
ImmutableSeries<T, D> get series;
D get datum;
T get datum;
Rectangle<int> get bounds;
}

class _BarRendererElement<T, D> extends BaseBarRendererElement
implements ImmutableBarRendererElement {
implements ImmutableBarRendererElement<T, D> {
ImmutableSeries<T, D> series;
D datum;
T datum;
Rectangle<int> bounds;
int roundPx;

Expand Down Expand Up @@ -309,7 +309,8 @@ class _BarRendererElement<T, D> extends BaseBarRendererElement
}
}

class _AnimatedBar<T, D> extends BaseAnimatedBar<T, D, _BarRendererElement> {
class _AnimatedBar<T, D>
extends BaseAnimatedBar<T, D, _BarRendererElement<T, D>> {
_AnimatedBar(
{@required String key,
@required T datum,
Expand All @@ -329,8 +330,8 @@ class _AnimatedBar<T, D> extends BaseAnimatedBar<T, D, _BarRendererElement> {
0);
}

_BarRendererElement getCurrentBar(double animationPercent) {
final _BarRendererElement bar = super.getCurrentBar(animationPercent);
_BarRendererElement<T, D> getCurrentBar(double animationPercent) {
final _BarRendererElement<T, D> bar = super.getCurrentBar(animationPercent);

// Update with series and datum information to pass to bar decorator.
bar.series = series;
Expand All @@ -340,6 +341,6 @@ class _AnimatedBar<T, D> extends BaseAnimatedBar<T, D, _BarRendererElement> {
}

@override
_BarRendererElement clone(_BarRendererElement other) =>
new _BarRendererElement.clone(other);
_BarRendererElement<T, D> clone(_BarRendererElement other) =>
new _BarRendererElement<T, D>.clone(other);
}
6 changes: 3 additions & 3 deletions charts_common/lib/src/chart/bar/bar_renderer_config.dart
Expand Up @@ -21,7 +21,7 @@ import '../common/chart_canvas.dart' show FillPatternType;
import '../../common/symbol_renderer.dart';

/// Configuration for a bar renderer.
class BarRendererConfig extends BaseBarRendererConfig<String> {
class BarRendererConfig<T> extends BaseBarRendererConfig<T, String> {
/// Calculator for determining the corner radius of a bar.
final CornerRadiusCalculator cornerRadiusCalculator;

Expand Down Expand Up @@ -50,8 +50,8 @@ class BarRendererConfig extends BaseBarRendererConfig<String> {
symbolRenderer: symbolRenderer);

@override
BarRenderer<dynamic, String> build() {
return new BarRenderer<dynamic, String>(
BarRenderer<T, String> build() {
return new BarRenderer<T, String>(
config: this, rendererId: customRendererId);
}

Expand Down
4 changes: 2 additions & 2 deletions charts_common/lib/src/chart/bar/bar_renderer_decorator.dart
Expand Up @@ -20,10 +20,10 @@ import '../../common/graphics_factory.dart' show GraphicsFactory;
import 'bar_renderer.dart' show ImmutableBarRendererElement;

/// Decorates bars after the bars have already been painted.
abstract class BarRendererDecorator {
abstract class BarRendererDecorator<T, D> {
const BarRendererDecorator();

void decorate(Iterable<ImmutableBarRendererElement> barElements,
void decorate(Iterable<ImmutableBarRendererElement<T, D>> barElements,
ChartCanvas canvas, GraphicsFactory graphicsFactory,
{@required Rectangle drawBounds,
@required double animationPercent,
Expand Down
Expand Up @@ -19,7 +19,7 @@ import 'bar_target_line_renderer.dart' show BarTargetLineRenderer;
import '../common/chart_canvas.dart' show FillPatternType;

/// Configuration for a bar target line renderer.
class BarTargetLineRendererConfig extends BaseBarRendererConfig<String> {
class BarTargetLineRendererConfig<T> extends BaseBarRendererConfig<T, String> {
/// The number of pixels that the line will extend beyond the bandwidth at the
/// edges of the bar group.
///
Expand Down Expand Up @@ -57,8 +57,8 @@ class BarTargetLineRendererConfig extends BaseBarRendererConfig<String> {
strokeWidthPx: strokeWidthPx);

@override
BarTargetLineRenderer<dynamic, String> build() {
return new BarTargetLineRenderer<dynamic, String>(
BarTargetLineRenderer<T, String> build() {
return new BarTargetLineRenderer<T, String>(
config: this, rendererId: customRendererId);
}

Expand Down
4 changes: 2 additions & 2 deletions charts_common/lib/src/chart/bar/base_bar_renderer_config.dart
Expand Up @@ -38,8 +38,8 @@ import '../../common/symbol_renderer.dart' show SymbolRenderer;
/// offsets overlap. Note that bars for each series will be rendered in order,
/// such that bars from the last series will be "on top" of bars from previous
/// series.
abstract class BaseBarRendererConfig<D> extends LayoutViewConfig
implements SeriesRendererConfig<dynamic, D> {
abstract class BaseBarRendererConfig<T, D> extends LayoutViewConfig
implements SeriesRendererConfig<T, D> {
final String customRendererId;

final SymbolRenderer symbolRenderer;
Expand Down
34 changes: 34 additions & 0 deletions charts_common/lib/src/chart/cartesian/cartesian_chart.dart
Expand Up @@ -30,8 +30,10 @@ import 'axis/draw_strategy/gridline_draw_strategy.dart'
import '../bar/bar_renderer.dart' show BarRenderer;
import '../common/base_chart.dart' show BaseChart;
import '../common/chart_context.dart' show ChartContext;
import '../common/datum_details.dart' show DatumDetails;
import '../common/processed_series.dart' show MutableSeries;
import '../common/series_renderer.dart' show SeriesRenderer;
import '../common/selection_model/selection_model.dart' show SelectionModelType;
import '../layout/layout_config.dart' show LayoutConfig, MarginSpec;
import '../../common/graphics_factory.dart' show GraphicsFactory;
import '../../common/rtl_spec.dart' show AxisPosition;
Expand Down Expand Up @@ -228,4 +230,36 @@ abstract class CartesianChart<T, D> extends BaseChart<T, D> {

super.onPostLayout(rendererToSeriesList);
}

/// Returns a list of datum details from selection model of [type].
@override
List<DatumDetails<T, D>> getDatumDetails(SelectionModelType type) {
final entries = <DatumDetails<T, D>>[];

getSelectionModel(type).selectedDatum.forEach((seriesDatum) {
final series = seriesDatum.series;
final datum = seriesDatum.datum;

final domain = series.domainFn(datum, null);
final measure = series.measureFn(datum, null);
final color = series.colorFn(datum, null);

final x =
series.getAttr(domainAxisKey).getLocation(series.domainFn(datum, -1));
final y = series
.getAttr(measureAxisKey)
.getLocation(series.measureFn(datum, -1));

entries.add(new DatumDetails(
datum: datum,
domain: domain,
measure: measure,
series: series,
color: color,
chartX: x,
chartY: y));
});

return entries;
}
}
9 changes: 9 additions & 0 deletions charts_common/lib/src/chart/common/base_chart.dart
Expand Up @@ -118,6 +118,9 @@ abstract class BaseChart<T, D> {
return _selectionModels.putIfAbsent(type, () => new SelectionModel<T, D>());
}

/// Returns a list of datum details from selection model of [type].
List<DatumDetails<T, D>> getDatumDetails(SelectionModelType type);

//
// Renderer methods
//
Expand Down Expand Up @@ -294,6 +297,12 @@ abstract class BaseChart<T, D> {
// Allow listeners to manipulate the seriesList.
fireOnDraw(processedSeriesList);

// Set an index on the series list.
// This can be used by listeners of selection to determine the order of
// series, because the selection details are not returned in this order.
int seriesIndex = 0;
processedSeriesList.forEach((series) => series.seriesIndex = seriesIndex++);

_currentSeriesList = processedSeriesList;

drawInternal(processedSeriesList, skipAnimation: false, skipLayout: false);
Expand Down
41 changes: 41 additions & 0 deletions charts_common/lib/src/chart/common/datum_details.dart
Expand Up @@ -13,19 +13,60 @@
// See the License for the specific language governing permissions and
// limitations under the License.

import '../../common/color.dart' show Color;
import 'processed_series.dart' show ImmutableSeries;

typedef DomainFormatter<D>(D domain);
typedef MeasureFormatter(num measure);

class DatumDetails<T, D> {
final T datum;

/// Domain value of [datum].
final D domain;

/// Measure value of [datum].
final num measure;

/// The series the [datum] is from.
final ImmutableSeries<T, D> series;

/// The color of this [datum].
final Color color;

/// The chart X coordinate for the [datum] from a renderer.
final double chartX;

/// The chart Y coordinate for the [datum] from a renderer.
final double chartY;

/// Distance of [domain] from a given x, y coordinates.
final double domainDistance;

/// Distance of [measure] from a given x, y coordinates.
final double measureDistance;

/// Optional formatter for [domain].
DomainFormatter<D> domainFormatter;

/// Optional formatter for [measure].
MeasureFormatter measureFormatter;

DatumDetails(
{this.datum,
this.domain,
this.measure,
this.series,
this.color,
this.chartX,
this.chartY,
this.domainDistance,
this.measureDistance});

String get formattedDomain =>
(domainFormatter != null) ? domainFormatter(domain) : domain.toString();

String get formattedMeasure => (measureFormatter != null)
? measureFormatter(measure)
: measure.toString();
}
3 changes: 3 additions & 0 deletions charts_common/lib/src/chart/common/processed_series.dart
Expand Up @@ -24,6 +24,7 @@ class MutableSeries<T, D> extends ImmutableSeries<T, D> {
String displayName;
String seriesCategory;
bool overlaySeries;
int seriesIndex;

List<T> data;

Expand Down Expand Up @@ -74,6 +75,7 @@ class MutableSeries<T, D> extends ImmutableSeries<T, D> {
displayName = other.displayName;
seriesCategory = other.seriesCategory;
overlaySeries = other.overlaySeries;
seriesIndex = other.seriesIndex;

data = other.data;
domainFn = other.domainFn;
Expand Down Expand Up @@ -116,6 +118,7 @@ abstract class ImmutableSeries<T, D> {
String get displayName;
String get seriesCategory;
bool get overlaySeries;
int get seriesIndex;

List<T> get data;

Expand Down
4 changes: 2 additions & 2 deletions charts_common/lib/src/chart/common/series_renderer.dart
Expand Up @@ -98,7 +98,7 @@ abstract class BaseSeriesRenderer<T, D> implements SeriesRenderer<T, D> {
/// Setting it to false used different palettes (ie: s1 uses Blue500,
/// s2 uses Red500),
@protected
assignMissingColors(Iterable<MutableSeries> seriesList,
assignMissingColors(Iterable<MutableSeries<T, D>> seriesList,
{@required bool emptyCategoryUsesSinglePalette}) {
const defaultCategory = '__default__';

Expand All @@ -108,7 +108,7 @@ abstract class BaseSeriesRenderer<T, D> implements SeriesRenderer<T, D> {
int maxMissing = 0;
bool hasSpecifiedCategory = false;

seriesList.forEach((MutableSeries series) {
seriesList.forEach((MutableSeries<T, D> series) {
if (series.colorFn == null) {
// If there is no category, give it a default category to match logic.
String category = series.seriesCategory;
Expand Down

0 comments on commit be1bc84

Please sign in to comment.