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

Feature/category counts #139

Open
wants to merge 2 commits into
base: master
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
3 changes: 2 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -327,6 +327,7 @@ Object properties:
"property":"e.g. field_name_count",
"pallete":"schemeSet1 or ['list','of','colors']",
"spacing":30,
"categoryCount":false
}
```

Expand All @@ -336,7 +337,7 @@ Object properties:
| `property` | String | Required | Layer's property/field value to be used for calculating color, the field must be of categorical values. |
| `pallete` | Optional | Required | d3's categorical color sets colors e.g. `schemeSet1` or you can give your own list of color strings as defined in the `consts.js` file defaults to `schemePastel1`.
| `spacing` | Optional | Required | A number of pixels to define the width of each categorical legend color circle and text entry; defaults to **14 + (label length x 10) px**, which allows for short labels to display inline. To force only one label per line, try **240** px.|

| `categoryCount` | Boolean | Optional | If true, the feature count for each category will be included on the legend. Defaults to `false` |


### H3HexLayer Options
Expand Down
4 changes: 2 additions & 2 deletions dist/lbh-webmap.min.js

Large diffs are not rendered by default.

7 changes: 6 additions & 1 deletion src/js/helpers/dynamic-styles.js
Original file line number Diff line number Diff line change
Expand Up @@ -63,4 +63,9 @@ function getCategoryColor (data,colorPalette="schemePastel1"){

}

export { getFeatureData,getMinMax,createBins,getScaleRange,colorInterpolator,getDistinctValues,getCategoryColor};
function getCategoryCounts(data,property,category){
const numberOfFeatures = data.features.filter(feature => feature.properties?.[property] == category && feature.geometry)
return ` (${numberOfFeatures?.length})`
}

export { getFeatureData,getMinMax,createBins,getScaleRange,colorInterpolator,getDistinctValues,getCategoryColor,getCategoryCounts};
8 changes: 5 additions & 3 deletions src/js/map/data-layers.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ import Table from "./table-view.js";
import DataDownload from "./data-download.js";
import Accessibility from "./accessiblity.js";
import { getFeatureData,getMinMax,createBins,getScaleRange,
colorInterpolator,getDistinctValues,getCategoryColor } from "../helpers/dynamic-styles.js";
colorInterpolator,getDistinctValues,getCategoryColor,getCategoryCounts } from "../helpers/dynamic-styles.js";
import createH3Geojson from "../helpers/h3-layer.js";
import { color } from "d3";

Expand Down Expand Up @@ -177,6 +177,8 @@ class DataLayers {
}

addWFSLayer(data, configLayer) {
// filter out null geometries
data.features = data.features.filter(feature => feature?.geometry)
const layerName = configLayer.title;
const sortOrder =
configLayer.sortOrder && !isNaN(configLayer.sortOrder)
Expand Down Expand Up @@ -237,15 +239,15 @@ class DataLayers {
${scaleRange.map((x,i)=> i % 2 == 0 || i==0 || scaleRange.length-1==i ?`<text x=${i>0?i*rangeLegendSpacing-5:i*rangeLegendSpacing} y="20" font-size="12" font-weight="bold">${x}</text>`:'')}
</svg>`
const scaleLegendTitle = scaleLegend && rangeStyle.legendTitle

//categoryStyle
const categoryStyle = configLayer.categoryStyle;
const categoryCount = categoryStyle?.categoryCount;
const categories = categoryStyle && getDistinctValues(data,categoryStyle.property)
const colorPicker = categories && getCategoryColor(categories,categoryStyle.palette||"schemePastel1")
const categoryLegend = categories && `<div class="categorical-legend control__count">
${categories.map((category)=>`<svg width="${categoryStyle.spacing||14+category.length*10}" height="16">
<circle cx="7" cy="7" r="7" fill="${colorPicker(category)}"></circle>
<text x="25" y="12" font-size="12" font-weight="medium">${category}</text>
<text x="25" y="12" font-size="12" font-weight="medium">${category}${categoryCount?getCategoryCounts(data,categoryStyle.property,category):''}</text>
</svg>`).join("")}
</div>`

Expand Down