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

adding new files #33

Open
wants to merge 1 commit into
base: new-codelabs
Choose a base branch
from
Open
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
2 changes: 2 additions & 0 deletions barchart-codelab/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
node_modules/
build/
3 changes: 3 additions & 0 deletions barchart-codelab/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
# Community Viz Barchart Codelab

Code for dscc-gen barchart codelab
14 changes: 14 additions & 0 deletions barchart-codelab/dist/index.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
<!doctype html>
<html>

<head>
<title>Community Viz</title>

musiciancodes marked this conversation as resolved.
Show resolved Hide resolved
</head>

<body>
<iframe src="vizframe.html" componentid="abc" width="75%" height="800px">
</iframe>
</body>

</html>
23 changes: 23 additions & 0 deletions barchart-codelab/dist/vizframe.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
<!doctype html>
<html>

<head>
<link href="https://fonts.googleapis.com/css?family=Roboto" rel="stylesheet">
<style>
body {
font-family: Roboto;
}

code {
background-color: #fdf6e3;
color: #586e75;
}
</style>
</head>

<body>
<script src="main.js"></script>
<link rel="stylesheet" href="index.css">
</body>

</html>
28 changes: 28 additions & 0 deletions barchart-codelab/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
{
"dsccViz": {
"gcsDevBucket": "gs://yl-viz-dev/barchart-codelab",
"gcsProdBucket": "gs://yl-viz-dev/barchart-codelab-prod",
"jsFile": "index.js",
"jsonFile": "index.json",
"cssFile": "index.css",
"print": "printMessage.js"
},
"scripts": {
"build:dev": "dscc-scripts viz build -d dev",
"prettier": "prettier --write \"**/*.js\"",
"build:prod": "dscc-scripts viz build -d prod",
"push:dev": "dscc-scripts viz push -d dev",
"push:prod": "dscc-scripts viz push -d prod",
"update_message": "dscc-scripts viz update_message -f object",
"start": "dscc-scripts viz start"
},
"devDependencies": {
"@google/dscc": "^0.3.13",
"@google/dscc-scripts": "^1.0.14"
},
"dependencies": {
"d3": "^5.15.0",
"d3-array": "^2.4.0",
"prettier": "^1.19.1"
}
}
Empty file added barchart-codelab/src/index.css
Empty file.
91 changes: 91 additions & 0 deletions barchart-codelab/src/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,91 @@
const dscc = require("@google/dscc");
const d3 = require("d3");

// parse the style value
const styleVal = (message, styleId) => {
musiciancodes marked this conversation as resolved.
Show resolved Hide resolved
if (typeof message.style[styleId].defaultValue === "object") {
musiciancodes marked this conversation as resolved.
Show resolved Hide resolved
return message.style[styleId].value.color !== undefined
? message.style[styleId].value.color
: message.style[styleId].defaultValue.color;
}
return message.style[styleId].value !== undefined
? message.style[styleId].value
: message.style[styleId].defaultValue;
};

const drawViz = messsage => {
musiciancodes marked this conversation as resolved.
Show resolved Hide resolved
const margin = { left: 20, right: 20, top: 20, bottom: 20 };
const height = dscc.getHeight() - 10;
const width = dscc.getWidth();

const chartHeight = height - margin.top - margin.bottom;
const chartWidth = width - margin.left - margin.right;

// remove existing svg
d3.select("body")
.selectAll("svg")
.remove();

// make a canvas
const svg = d3
musiciancodes marked this conversation as resolved.
Show resolved Hide resolved
.select("body")
.append("svg")
.attr("width", width)
.attr("height", height);

// make an svg for the bar chart
const chartSvg = svg
.append("svg")
.attr("x", margin.left)
musiciancodes marked this conversation as resolved.
Show resolved Hide resolved
.attr("y", margin.top)
.attr("width", chartWidth)
.attr("height", chartHeight);

// xScale to distribute bars
musiciancodes marked this conversation as resolved.
Show resolved Hide resolved
const xScale = d3
.scaleBand()
.domain(messsage.tables.DEFAULT.map(d => d.dimension[0]))
.range([0, chartWidth])
.paddingInner(0.3);

// yScale to size bars
musiciancodes marked this conversation as resolved.
Show resolved Hide resolved
const yScale = d3
.scaleLinear()
.domain([0, d3.max(messsage.tables.DEFAULT.map(d => d.metric[0]))])
.range([0, chartHeight]);

// get the user-selected bar color
musiciancodes marked this conversation as resolved.
Show resolved Hide resolved
let barColor = styleVal(messsage, "barColor");

// add bars
const bars = chartSvg
.append("g")
.attr("class", "bars")
.selectAll("rect.bars")
.data(messsage.tables.DEFAULT)
.enter()
.append("rect")
.attr("x", d => xScale(d.dimension[0]))
musiciancodes marked this conversation as resolved.
Show resolved Hide resolved
.attr("y", d => chartHeight - yScale(d.metric[0]))
.attr("width", xScale.bandwidth())
.attr("height", d => yScale(d.metric[0]))
.attr("fill", barColor);

// add text
musiciancodes marked this conversation as resolved.
Show resolved Hide resolved
const text = svg
.append("g")
.selectAll("text")
.data(messsage.tables.DEFAULT)
.enter()
.append("text")
.attr(
"x",
d => xScale(d.dimension[0]) + xScale.bandwidth() / 2 + margin.left
)
.attr("y", height - margin.bottom / 4)
.attr("text-anchor", "middle")
.attr("fill", barColor)
.text(d => d.dimension[0]);
};

dscc.subscribeToData(drawViz, { transform: dscc.objectTransform });
44 changes: 44 additions & 0 deletions barchart-codelab/src/index.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
{
"data": [
{
"id": "concepts",
"label": "Concepts",
"elements": [
{
"id": "dimension",
"label": "Dimensions",
"type": "DIMENSION",
"options": {
"min": 1,
"max": 1
}
},
{
"id": "metric",
"label": "Metric",
"type": "METRIC",
"options": {
"min": 1,
"max": 1
}
}
]
}
],
"style": [
{
"id": "barStyling",
"label": "Bar Styles",
"elements": [
{
"id": "barColor",
"label": "Bar Color",
"type": "FILL_COLOR",
"defaultValue": {
"color": "#1E555C"
}
}
]
}
]
}
23 changes: 23 additions & 0 deletions barchart-codelab/src/manifest.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
{
"name": "My Visualizations",
"organization": "MyOrg",
"description": "My first visualization package.",
"logoUrl": "https://logo",
"organizationUrl": "https://url",
"supportUrl": "https://url",
"privacyPolicyUrl": "https://url",
"termsOfServiceUrl": "https://url",
"packageUrl": "https://url",
"devMode": "DEVMODE_BOOL",
"components": [
{
"name": "table",
"description": "Simple JavaScript Table",
"iconUrl": "https://url",
"resource": {
"js": "YOUR_GCS_BUCKET/index.js",
"config": "YOUR_GCS_BUCKET/index.json"
}
}
]
}
21 changes: 21 additions & 0 deletions barchart-codelab/webpack.config.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
const path = require("path");
musiciancodes marked this conversation as resolved.
Show resolved Hide resolved
const CopyWebpackPlugin = require("copy-webpack-plugin");

const CSS_FILE = process.env.npm_package_dsccViz_cssFile;

module.exports = [
{
mode: "development",
entry: "./src/index.js",
devServer: {
contentBase: "./dist"
},
output: {
filename: "main.js",
path: path.resolve(__dirname, "dist")
},
plugins: [
new CopyWebpackPlugin([{ from: path.join("src", CSS_FILE), to: "." }])
]
}
];
Loading