-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #35 from shotnothing/price-bar
Price bar
- Loading branch information
Showing
14 changed files
with
4,235 additions
and
210 deletions.
There are no files selected for viewing
Large diffs are not rendered by default.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
22 changes: 22 additions & 0 deletions
22
webapp/src/app/(authenticated)/pricebar/components/boxplot-legend.css
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
.legend { | ||
display: flex; | ||
flex-direction: column; | ||
} | ||
|
||
.legend-item { | ||
display: flex; | ||
align-items: center; | ||
margin-bottom: 5px; | ||
} | ||
|
||
.circle { | ||
width: 10px; | ||
height: 10px; | ||
border-radius: 50%; | ||
margin-right: 5px; | ||
} | ||
|
||
.label { | ||
font-size: 14px; | ||
} | ||
|
27 changes: 27 additions & 0 deletions
27
webapp/src/app/(authenticated)/pricebar/components/boxplot-legend.tsx
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
import React from 'react'; | ||
import './boxplot-legend.css'; | ||
|
||
|
||
interface LegendItem { | ||
label: string; | ||
color: string; | ||
} | ||
|
||
interface BoxPlotLegendProps { | ||
legendItems: LegendItem[]; | ||
} | ||
|
||
const BoxPlotLegend: React.FC<BoxPlotLegendProps> = ({ legendItems }) => { | ||
return ( | ||
<div className="legend"> | ||
{legendItems.map((item, index) => ( | ||
<div key={index} className="legend-item"> | ||
<span className="circle" style={{ backgroundColor: item.color }}></span> | ||
<span className="label">{item.label}</span> | ||
</div> | ||
))} | ||
</div> | ||
); | ||
}; | ||
|
||
export default BoxPlotLegend; |
141 changes: 141 additions & 0 deletions
141
webapp/src/app/(authenticated)/pricebar/components/boxplot.tsx
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,141 @@ | ||
"use client" | ||
|
||
import React, { useEffect, useRef } from 'react'; | ||
import * as d3 from 'd3'; | ||
|
||
interface BoxPlotProps { | ||
data: { [key: string]: number }; | ||
width: number; | ||
height: number; | ||
svgRef: React.MutableRefObject<SVGSVGElement | null>; | ||
} | ||
|
||
const BoxPlot: React.FC<BoxPlotProps> = ({ data, width, height, svgRef }) => { | ||
useEffect(() => { | ||
if (!svgRef.current) return; | ||
|
||
const dataArray = Object.values(data); | ||
if (dataArray.length === 0) return; | ||
|
||
const margin = { top: 20, right: 50, bottom: 50, left: 2 }; | ||
const innerWidth = width - margin.left - margin.right; | ||
const innerHeight = height - margin.top - margin.bottom; | ||
|
||
const svg = d3.select(svgRef.current) | ||
.attr('width', width) | ||
.attr('height', height) | ||
.append('g') | ||
.attr('transform', `translate(${margin.left}, ${margin.top})`); | ||
|
||
const yScale = d3.scaleBand() | ||
.domain(['Box Plot']) | ||
.range([margin.top, innerHeight]) | ||
.padding(0.1); | ||
|
||
const xScale = d3.scaleLinear() | ||
.domain([d3.min(dataArray)!, d3.max(dataArray)!]) | ||
.nice() | ||
.range([margin.left, innerWidth]); | ||
|
||
const g = svg.append('g') | ||
.attr('transform', `translate(${margin.left},${margin.top})`); | ||
|
||
// Draw box plot | ||
const boxPlotGroup = g.append('g'); | ||
|
||
const boxHeight = yScale.bandwidth(); | ||
|
||
Object.entries(data).forEach(([key, value]) => { | ||
// For each key-value pair in the data object | ||
boxPlotGroup.append('rect') | ||
.attr('y', yScale('Box Plot') - 20) | ||
.attr('x', xScale(d3.quantile(dataArray, 0.25))) | ||
.attr('height', 40) | ||
.attr('width', xScale(d3.quantile(dataArray, 0.75)) - xScale(d3.quantile(dataArray, 0.25))) | ||
.attr('fill', 'rgba(0, 0, 0, 0.01)'); | ||
}); | ||
|
||
|
||
boxPlotGroup.append('line') | ||
.attr('y1', yScale('Box Plot') - 20) | ||
.attr('y2', yScale('Box Plot') + 20) | ||
.attr('x1', xScale(d3.quantile(dataArray, 0.5))) | ||
.attr('x2', xScale(d3.quantile(dataArray, 0.5))) | ||
.attr('stroke', 'black'); | ||
|
||
boxPlotGroup.append('line') | ||
.attr('y1', yScale('Box Plot') - 20) | ||
.attr('y2', yScale('Box Plot') + 20) | ||
.attr('x1', xScale(d3.min(dataArray)!)) | ||
.attr('x2', xScale(d3.min(dataArray)!)) | ||
.attr('stroke', 'black') | ||
.attr('stroke-width', 0.5) | ||
.attr('stroke-dasharray', '3'); | ||
|
||
boxPlotGroup.append('line') | ||
.attr('y1', yScale('Box Plot') - 20) | ||
.attr('y2', yScale('Box Plot') + 20) | ||
.attr('x1', xScale(d3.max(dataArray)!)) | ||
.attr('x2', xScale(d3.max(dataArray)!)) | ||
.attr('stroke', 'black') | ||
.attr('stroke-width', 0.5) | ||
.attr('stroke-dasharray', '3'); | ||
|
||
boxPlotGroup.append('line') | ||
.attr('y1', yScale('Box Plot') - 20) | ||
.attr('y2', yScale('Box Plot') + 20) | ||
.attr('x1', xScale(d3.quantile(dataArray, 0.25))) | ||
.attr('x2', xScale(d3.quantile(dataArray, 0.25))) | ||
.attr('stroke', 'black') | ||
|
||
|
||
boxPlotGroup.append('line') | ||
.attr('y1', yScale('Box Plot') - 20) | ||
.attr('y2', yScale('Box Plot') + 20) | ||
.attr('x1', xScale(d3.quantile(dataArray, 0.75))) | ||
.attr('x2', xScale(d3.quantile(dataArray, 0.75))) | ||
.attr('stroke', 'black') | ||
|
||
boxPlotGroup.append('line') | ||
.attr('y1', yScale('Box Plot')) | ||
.attr('y2', yScale('Box Plot')) | ||
.attr('x1', xScale(d3.min(dataArray)!)) | ||
.attr('x2', xScale(d3.quantile(dataArray, 0.25))) | ||
.attr('stroke', 'black') | ||
|
||
boxPlotGroup.append('line') | ||
.attr('y1', yScale('Box Plot')) | ||
.attr('y2', yScale('Box Plot')) | ||
.attr('x1', xScale(d3.quantile(dataArray, 0.75))) | ||
.attr('x2', xScale(d3.max(dataArray)!)) | ||
.attr('stroke', 'black') | ||
|
||
boxPlotGroup.append('line') | ||
.attr('y1', yScale('Box Plot')-20) | ||
.attr('y2', yScale('Box Plot')-20) | ||
.attr('x1', xScale(d3.quantile(dataArray, 0.25))) | ||
.attr('x2', xScale(d3.quantile(dataArray, 0.75))) | ||
.attr('stroke', 'black') | ||
|
||
boxPlotGroup.append('line') | ||
.attr('y1', yScale('Box Plot')+20) | ||
.attr('y2', yScale('Box Plot')+20) | ||
.attr('x1', xScale(d3.quantile(dataArray, 0.25))) | ||
.attr('x2', xScale(d3.quantile(dataArray, 0.75))) | ||
.attr('stroke', 'black') | ||
|
||
const xAxis = d3.axisBottom(xScale); | ||
|
||
g.append('g') | ||
.attr('class', 'x-axis') | ||
.call(xAxis) | ||
.attr("transform", `translate(0, ${innerHeight})`); | ||
|
||
}, [data, width, height, svgRef]); | ||
|
||
return ( | ||
<svg ref={svgRef}></svg> | ||
); | ||
} | ||
|
||
export default BoxPlot; |
65 changes: 65 additions & 0 deletions
65
webapp/src/app/(authenticated)/pricebar/components/currentprices-mflg.tsx
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,65 @@ | ||
"use client" | ||
|
||
import React, { useEffect, useRef } from 'react'; | ||
import * as d3 from 'd3'; | ||
|
||
|
||
interface CurrentPricesProps { | ||
data: { [key: string]: number }; | ||
width: number; | ||
height: number; | ||
svgRef: React.MutableRefObject<SVGSVGElement | null>; | ||
} | ||
|
||
const CurrentPricesMFLG:React.FC<CurrentPricesProps> = ({ data, width, height, svgRef }) => { | ||
|
||
useEffect(() => { | ||
const dataArray = Object.values(data); // Extract values from the object | ||
|
||
|
||
if (dataArray.length === 0) return; | ||
const margin = { top: 20, right: 50, bottom: 50, left: 2 }; | ||
const innerWidth = width - margin.left - margin.right; | ||
const innerHeight = height - margin.top - margin.bottom; | ||
|
||
const svg = d3.select(svgRef.current) | ||
.attr('width', width) | ||
.attr('height', height) | ||
.append('g') | ||
.attr('transform', `translate(${margin.left}, ${margin.top})`); | ||
|
||
const yScale = d3.scaleBand() | ||
.domain(['Box Plot']) | ||
.range([margin.top, innerHeight]) | ||
.padding(0.1); | ||
|
||
const xScale = d3.scaleLinear() | ||
.domain([d3.min(dataArray)!, d3.max(dataArray)!]) | ||
.nice() | ||
.range([margin.left, innerWidth]); | ||
|
||
const g = svg.append('g') | ||
.attr('transform', `translate(${margin.left},${margin.top})`); | ||
|
||
// Draw box plot | ||
const boxPlotGroup = g.append('g'); | ||
|
||
const boxHeight = yScale.bandwidth(); | ||
|
||
|
||
Object.entries(data).forEach(([key, value]) => { | ||
if (key === 'mflg') | ||
boxPlotGroup.append('circle') | ||
.attr('cy', yScale('Box Plot')) | ||
.attr('cx', xScale(value)) | ||
.attr('r', 6) | ||
.attr('fill', 'green'); | ||
}); | ||
|
||
|
||
}, [data, width, height, svgRef]); | ||
|
||
return null; | ||
} | ||
|
||
export default CurrentPricesMFLG; |
8 changes: 8 additions & 0 deletions
8
webapp/src/app/(authenticated)/pricebar/components/currentprices.css
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
.tooltip { | ||
position: absolute; | ||
background-color: white; | ||
border: 1px solid #ccc; | ||
padding: 5px; | ||
border-radius: 5px; | ||
pointer-events: none; | ||
} |
Oops, something went wrong.