Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

BulletLegend improvements and new scatter plot examples #326

Merged
merged 5 commits into from
Jan 24, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { Component, AfterViewInit, Input, SimpleChanges, ViewChild, ElementRef } from '@angular/core'
import { BulletLegend, BulletLegendConfigInterface, BulletLegendItemInterface, BulletShape } from '@unovis/ts'
import { BulletLegend, BulletLegendConfigInterface, BulletLegendItemInterface, BulletShape, GenericAccessor } from '@unovis/ts'
import { VisGenericComponent } from '../../core'

@Component({
Expand Down Expand Up @@ -39,8 +39,8 @@ export class VisBulletLegendComponent implements BulletLegendConfigInterface, Af
/** Bullet circle size, mapped to the width and height CSS properties. Default: `null` */
@Input() bulletSize?: string | null

/** Bullet shape: `BulletShape.Circle`, `BulletShape.Line` or `BulletShape.Square`. Default: `BulletShape.Circle` */
@Input() bulletShape?: BulletShape
/** Bullet shape enum value or accessor function. Default: `d => d.shape ?? BulletShape.Circle */
@Input() bulletShape?: GenericAccessor<BulletShape, BulletLegendItemInterface>

component: BulletLegend | undefined

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
import React, { useState, useCallback, useEffect } from 'react'
import { BulletLegendItemInterface, BulletShape } from '@unovis/ts'
import { VisBulletLegend, VisXYContainer, VisScatter, VisAxis } from '@unovis/react'
import { generateStackedDataRecords, StackedDataRecord } from '@src/utils/data'

export const title = 'Shape Legend'
export const subTitle = 'with Scatter Plot'

const STACKED = 7
const data = generateStackedDataRecords(10, STACKED)
const items = Array(STACKED).fill(0).map((_, i) => ({ name: `Y${i}`, inactive: false }))
const shapes = Object.values(BulletShape)

export const component = (): JSX.Element => {
const x = (d: StackedDataRecord): number => d.x
const shape = (_, i: number): string => shapes[i % shapes.length]

const [accessors, setAccessors] = useState<(null | ((d: StackedDataRecord) => number))[]>()
const [legendItems, setLegendItems] = useState(items)

const toggleItem = useCallback((_: BulletLegendItemInterface, index: number) => {
const newItems = legendItems.map((l, i) => i === index ? ({ ...l, inactive: !l.inactive }) : l)
setLegendItems(newItems)
}, [legendItems])

useEffect(() =>
setAccessors(legendItems.map((l, i) => l.inactive ? null : (d: StackedDataRecord) => d.ys[i]))
, [legendItems])

return (<>
<VisBulletLegend
items={legendItems}
bulletShape={shape}
onLegendItemClick={toggleItem}
/>
<VisXYContainer>
<VisScatter data={data} x={x} y={accessors} shape={shape}/>
<VisAxis type='x'/>
<VisAxis type='y'/>
</VisXYContainer>
</>
)
}
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
<vis-bullet-legend [items]="legendItems"></vis-bullet-legend>
<vis-xy-container [data]="data" [height]="600">
<vis-scatter [x]="x" [y]="y" [color]="color" [size]="8"></vis-scatter>
<vis-axis type="x" label="Beak Length (mm)"></vis-axis>
<vis-axis type="y" label="Flipper Length (mm)"></vis-axis>
</vis-xy-container>
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
import { Component } from '@angular/core'
import type { BulletLegendItemInterface, NumericAccessor, StringAccessor } from '@unovis/ts'
import { data, DataRecord } from './data'

@Component({
selector: 'basic-scatter-plot',
templateUrl: './basic-scatter-plot.component.html',
})
export class BasicScatterPlotComponent {
data = data

legendItems: BulletLegendItemInterface[] = [
{ name: 'Male', color: '#1fc3aa' },
{ name: 'Female', color: '#8624F5' },
{ name: 'No Data', color: '#aaa' },
]

x: NumericAccessor<DataRecord> = d => d.beakLength
y: NumericAccessor<DataRecord> = d => d.flipperLength
color: StringAccessor<DataRecord> = d => this.legendItems.find(i => i.name === (d.sex ?? 'No Data'))?.color
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
import { NgModule } from '@angular/core'
import { VisXYContainerModule, VisScatterModule, VisAxisModule, VisBulletLegendModule } from '@unovis/angular'

import { BasicScatterPlotComponent } from './basic-scatter-plot.component'

@NgModule({
imports: [VisXYContainerModule, VisScatterModule, VisAxisModule, VisBulletLegendModule],
declarations: [BasicScatterPlotComponent],
exports: [BasicScatterPlotComponent],
})
export class BasicScatterPlotModule { }
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
<script lang='ts'>
import type { NumericAccessor, StringAccessor } from '@unovis/ts'
import { VisXYContainer, VisScatter, VisAxis, VisBulletLegend } from '@unovis/svelte'
import { data, DataRecord } from './data'

const legendItems = [
{ name: 'Male', color: '#1fc3aa' },
{ name: 'Female', color: '#8624F5' },
{ name: 'No Data', color: '#aaa' },
]

const x: NumericAccessor<DataRecord> = d => d.beakLength
const y: NumericAccessor<DataRecord> = d => d.flipperLength
const color: StringAccessor<DataRecord> = d => legendItems.find(i => i.name === (d.sex ?? 'No Data'))?.color

</script>

<VisBulletLegend items={legendItems}/>
<VisXYContainer data={data} height={'60vh'}>
<VisScatter x={x} y={y} size={8} color={color}/>
<VisAxis type='x' label={'Beak Length (mm)'} />
<VisAxis type='y' label={'Flipper Length (mm)'}/>
</VisXYContainer>
28 changes: 28 additions & 0 deletions packages/shared/examples/basic-scatter-plot/basic-scatter-plot.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
import { Axis, BulletLegend, Scatter, XYContainer } from '@unovis/ts'
import { data, DataRecord } from './data'

const container = document.getElementById('vis-container')

// Legend
const legend = new BulletLegend(container, {
items: [
{ name: 'Male', color: '#1fc3aa' },
{ name: 'Female', color: '#8624F5' },
{ name: 'No Data', color: '#aaa' },
],
})

// Chart
const chart = new XYContainer<DataRecord>(container, {
height: 600,
components: [
new Scatter({
x: (d: DataRecord) => d.beakLength,
y: (d: DataRecord) => d.flipperLength,
color: (d: DataRecord) => legend.config.items.find(i => i.name === (d.sex ?? 'No Data'))?.color,
size: 8,
}),
],
xAxis: new Axis({ label: 'Beak Length (mm)' }),
yAxis: new Axis({ label: 'Flipper Length (mm)' }),
}, data)
27 changes: 27 additions & 0 deletions packages/shared/examples/basic-scatter-plot/basic-scatter-plot.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
import React from 'react'
import { NumericAccessor, StringAccessor } from '@unovis/ts'
import { VisAxis, VisBulletLegend, VisScatter, VisXYContainer } from '@unovis/react'
import { data, DataRecord } from './data'

export default function BasicScatterPlot (): JSX.Element {
const legendItems = [
{ name: 'Male', color: '#1fc3aa' },
{ name: 'Female', color: '#8624F5' },
{ name: 'No Data', color: '#aaa' },
]

const x: NumericAccessor<DataRecord> = d => d.beakLength
const y: NumericAccessor<DataRecord> = d => d.flipperLength
const color: StringAccessor<DataRecord> = d => legendItems.find(i => i.name === (d.sex ?? 'No Data'))?.color

return (
<>
<VisBulletLegend items={legendItems}/>
<VisXYContainer data={data} height={'60vh'}>
<VisScatter x={x} y={y} size={8} color={color}/>
<VisAxis type='x' label={'Beak Length (mm)'} />
<VisAxis type='y' label={'Flipper Length (mm)'}/>
</VisXYContainer>
</>
)
}
24 changes: 24 additions & 0 deletions packages/shared/examples/basic-scatter-plot/basic-scatter-plot.vue
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
<script lang='ts'>
import type { NumericAccessor, StringAccessor } from '@unovis/ts'
import { VisXYContainer, VisScatter, VisAxis, VisBulletLegend } from '@unovis/svelte'
import { data, DataRecord } from './data'

const legendItems = [
{ name: 'Male', color: '#1fc3aa' },
{ name: 'Female', color: '#8624F5' },
{ name: 'No Data', color: '#aaa' },
]

const x: NumericAccessor<DataRecord> = d => d.beakLength
const y: NumericAccessor<DataRecord> = d => d.flipperLength
const color: StringAccessor<DataRecord> = d => legendItems.find(i => i.name === (d.sex ?? 'No Data'))?.color
</script>

<template>
<VisBulletLegend :items="legendItems"/>
<VisXYContainer :data="data" :height="600">
<VisScatter :x="x" :y="y" :color="color" :size="8"/>
<VisAxis type='x' label='Beak Length (mm)' />
<VisAxis type='y' label='Flipper Length (mm)'/>
</VisXYContainer>
</template>
Loading
Loading