color scales in graph

This commit is contained in:
Colin Megill
2017-11-06 00:11:50 -08:00
parent 735e81f90b
commit 93ba5ef2aa
2 changed files with 59 additions and 18 deletions
+27 -11
View File
@@ -1,5 +1,6 @@
import styles from "./graph.css"; import styles from "./graph.css";
import renderQueue from "../continuous/renderQueue"; import renderQueue from "../continuous/renderQueue";
import _ from "lodash";
const margin = {top: 20, right: 10, bottom: 30, left: 40}, const margin = {top: 20, right: 10, bottom: 30, left: 40},
width = 960 - margin.left - margin.right, width = 960 - margin.left - margin.right,
@@ -50,11 +51,22 @@ export const setupGraphElements = (data) => {
******************************************* *******************************************
******************************************/ ******************************************/
export const drawGraph = (data, context, expressionsCountsMap) => { export const drawGraph = (data, context, expressionsCountsMap, color, ranges, metadata) => {
/* clear canvas */ /* clear canvas */
context.clearRect(0, 0, width, height); context.clearRect(0, 0, width, height);
let scale = null; /* it could be 'by expression' and that's a special case */
if (
color &&
ranges[color].range /* set up a continuous scale */
) {
scale = d3.scaleLinear()
.domain([0, ranges[color].range.max])
.range([1,0])
}
/* ! create a scale to map between expression values and colors, remove to somewhere else */ /* ! create a scale to map between expression values and colors, remove to somewhere else */
// const expressionToColorScale = d3.scaleLinear() // const expressionToColorScale = d3.scaleLinear()
// .domain([0, expressionsCountsMap.maxValue]) // .domain([0, expressionsCountsMap.maxValue])
@@ -63,8 +75,6 @@ export const drawGraph = (data, context, expressionsCountsMap) => {
/* shuffle the data to overcome render order hiding cells */ /* shuffle the data to overcome render order hiding cells */
data = d3.shuffle(data); data = d3.shuffle(data);
console.log('drawing', data, context, expressionsCountsMap)
/* loop */ /* loop */
data.forEach((p, i) => { data.forEach((p, i) => {
context.beginPath(); context.beginPath();
@@ -78,16 +88,22 @@ export const drawGraph = (data, context, expressionsCountsMap) => {
); );
context.fill(); context.fill();
// if (i < 20) { if (i < 20) {
// console.log( // console.log(
// '0 to 1 scale', expressionToColorScale(expressionsCountsMap[p[0]]), // '0 to 1 scale', expressionToColorScale(expressionsCountsMap[p[0]]),
// 'color', d3.interpolateViridis(expressionToColorScale( // 'color', d3.interpolateViridis(expressionToColorScale(
// expressionsCountsMap[p[0]] // expressionsCountsMap[p[0]]
// )) // ))
// ) // )
// } // console.log('meta', _.find(metadata, {CellName: p[0]}), color, p)
}
// context.fillStyle = d3.interpolateViridis(expressionToColorScale( // context.fillStyle = d3.interpolateViridis(expressionToColorScale(
// expressionsCountsMap[p[0]] // expressionsCountsMap[p[0]]
// )); // ));
if (color) {
context.fillStyle = d3.interpolateViridis(scale(
_.find(metadata, {CellName: p[0]})[color] /* this.state.cells.metadata["23452345325"]["ERCC_reads"] = 20000 */
));
}
}); });
} }
+32 -7
View File
@@ -4,13 +4,19 @@ import styles from "./graph.css";
import {setupGraphElements, drawGraph} from "./drawGraph"; import {setupGraphElements, drawGraph} from "./drawGraph";
import SectionHeader from "../framework/sectionHeader"; import SectionHeader from "../framework/sectionHeader";
import { connect } from "react-redux"; import { connect } from "react-redux";
import ColorControl from "../controls/color";
@connect((state) => { @connect((state) => {
const vertices = state.cells.cells && state.cells.cells.data.graph ? state.cells.cells.data.graph : null; const vertices = state.cells.cells && state.cells.cells.data.graph ? state.cells.cells.data.graph : null;
const ranges = state.cells.cells && state.cells.cells.data.ranges ? state.cells.cells.data.ranges : null;
const metadata = state.cells.cells && state.cells.cells.data.metadata ? state.cells.cells.data.metadata : null;
return { return {
vertices ranges,
vertices,
metadata,
color: state.controls.color,
} }
}) })
class Graph extends React.Component { class Graph extends React.Component {
@@ -21,11 +27,11 @@ class Graph extends React.Component {
drawn: false, drawn: false,
svg: null, svg: null,
context: null, context: null,
shimmer: false,
}; };
} }
componentWillReceiveProps(nextProps) { componentWillReceiveProps(nextProps) {
console.log('le grpah', nextProps)
/* maybe should do a check here to confirm ref exists and pass it? */ /* maybe should do a check here to confirm ref exists and pass it? */
if ( if (
this.state.context && this.state.context &&
@@ -33,11 +39,29 @@ class Graph extends React.Component {
// nextProps.expressions && // nextProps.expressions &&
// nextProps.expressionsCountsMap && // nextProps.expressionsCountsMap &&
) { ) {
drawGraph( if (this.state.shimmer) {
nextProps.vertices, if (this.state.interval) { window.clearInterval(this.state.interval) }
this.state.context, const interval = window.setInterval(() => {
nextProps.expressionsCountsMap drawGraph(
); nextProps.vertices,
this.state.context,
nextProps.expressionsCountsMap,
nextProps.color,
nextProps.ranges, /* assumption that this exists if vertices does both are on cells */
nextProps.metadata,
)
}, 100)
this.setState({interval})
} else {
drawGraph(
nextProps.vertices,
this.state.context,
nextProps.expressionsCountsMap,
nextProps.color,
nextProps.ranges, /* assumption that this exists if vertices does both are on cells */
nextProps.metadata,
)
}
} }
} }
@@ -50,6 +74,7 @@ class Graph extends React.Component {
return ( return (
<div id="graphWrapper"> <div id="graphWrapper">
<SectionHeader text="Graph"/> <SectionHeader text="Graph"/>
<ColorControl/>
<div id="graphAttachPoint"> </div> <div id="graphAttachPoint"> </div>
</div> </div>
) )